Spaces:
Running
Running
File size: 1,544 Bytes
3cb5693 db478bb 3cb5693 65563b1 3cb5693 db478bb a7d44c2 a11089e 3cb5693 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import gradio as gr
from medirag.cache.local import SemanticCaching
from medirag.index.local import DailyMedIndexer
from medirag.rag.qa import RAG, DailyMedRetrieve
import dspy
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
# Initialize the components
data_dir = Path("data")
index_path = data_dir.joinpath("dm_spl_release_human_rx_part1")
indexer = DailyMedIndexer(persist_dir=index_path)
indexer.load_index()
rm = DailyMedRetrieve(daily_med_indexer=indexer)
turbo = dspy.OpenAI(model='gpt-3.5-turbo', max_tokens=4000)
dspy.settings.configure(lm=turbo, rm=rm)
rag = RAG(k=5)
sm = SemanticCaching(model_name='sentence-transformers/all-mpnet-base-v2', dimension=768,
json_file='rag_test_cache.json', cosine_threshold=.90, rag=rag)
sm.load_cache()
def ask_med_question(query):
response = sm.ask(query)
return response
# Set up the Gradio interface
with gr.Blocks() as app:
gr.Row([
gr.Markdown("# Medical RAG Question Answering")
])
gr.Row([
gr.Markdown("## Ask any question about medication usage and get answers based on DailyMed data.")
])
with gr.Row():
input_text = gr.Textbox(lines=2, placeholder="Enter your question about a drug...")
with gr.Row():
button = gr.Button("Submit")
with gr.Row():
output_text = gr.Textbox(interactive=False, label="Response", show_label=False,
lines=10)
button.click(fn=ask_med_question, inputs=input_text, outputs=output_text)
app.launch()
|