Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
+
# # Import embedding model
|
| 2 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 3 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 4 |
+
from langchain_core.prompts import PromptTemplate
|
| 5 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 6 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 7 |
import gradio as gr
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
from langchain_groq import ChatGroq
|
| 11 |
+
# Create a vector store...
|
| 12 |
+
from langchain_chroma import Chroma
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
vectorstore = Chroma(
|
| 16 |
+
collection_name="medical_dataset_store",
|
| 17 |
+
embedding_function=embed_model,
|
| 18 |
+
persist_directory="./",
|
| 19 |
+
)
|
| 20 |
+
vectorstore.get().keys()
|
| 21 |
+
|
| 22 |
+
# Load the dataset to be used.
|
| 23 |
+
context = pd.read_csv("./drugs_side_effects_drugs_com.csv")
|
| 24 |
+
|
| 25 |
+
# Because the vector store is empty... Add your context data.
|
| 26 |
+
vectorstore.add_texts(context)
|
| 27 |
+
|
| 28 |
+
retriever = vectorstore.as_retriever()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
template = ("""
|
| 32 |
+
You are a medical expert specializing in pharmacology.
|
| 33 |
+
Your task is to use the provided context to answer questions about drug side effects for patients.
|
| 34 |
+
Please follow these guidelines:
|
| 35 |
+
- Provide accurate and detailed answers based on the context.
|
| 36 |
+
- If you don't know the answer, clearly state that you don't know.
|
| 37 |
+
- Do not reference the context directly in your response; just provide the answer.
|
| 38 |
+
- Ensure your answers are clear, concise, and informative.
|
| 39 |
+
Context: {context}
|
| 40 |
+
Question: {question}
|
| 41 |
+
Answer:
|
| 42 |
+
""")
|
| 43 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 44 |
+
|
| 45 |
+
# Initialize the model
|
| 46 |
+
llm_model = ChatGroq(model="llama-3.3-70b-versatile", api_key=os.environ.get("GROQ_API_KEY"))
|
| 47 |
+
rag_chain = (
|
| 48 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 49 |
+
| rag_prompt
|
| 50 |
+
| llm_model
|
| 51 |
+
| StrOutputParser()
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
def rag_memory_stream(message, history):
|
| 55 |
+
partial_text = ""
|
| 56 |
+
for new_text in rag_chain.stream(message):
|
| 57 |
+
partial_text += new_text
|
| 58 |
+
yield partial_text
|
| 59 |
+
|
| 60 |
+
examples = [
|
| 61 |
+
"What is a drug ?",
|
| 62 |
+
"What are the side effects of lisinopril?"
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
description = "Real-Time AI-Powered Medical Assistant: Drug Side Effect Queries Chatbot"
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
title = "AI-Powered Medical Chatbot :) Try me!"
|
| 69 |
+
demo = gr.ChatInterface(fn=rag_memory_stream,
|
| 70 |
+
type="messages",
|
| 71 |
+
title=title,
|
| 72 |
+
description=description,
|
| 73 |
+
fill_height=True,
|
| 74 |
+
examples=examples,
|
| 75 |
+
theme="glass",
|
| 76 |
)
|
| 77 |
|
| 78 |
+
# Launch the application and make it sharable
|
| 79 |
+
demo.launch(share=True)
|
| 80 |
+
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
demo.launch()
|