Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,144 @@
|
|
| 1 |
-
import
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
demo = gr.
|
| 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 pandas as pd
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
df = pd.read_csv('./drugs_side_effects_drugs_com.csv')
|
| 5 |
|
| 6 |
+
df = df[['drug_name', 'medical_condition', 'side_effects']]
|
| 7 |
+
df.dropna(inplace=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
context_data = []
|
| 10 |
+
for i in range(len(df)):
|
| 11 |
+
context = ""
|
| 12 |
+
for j in range(3):
|
| 13 |
+
context += df.columns[j]
|
| 14 |
+
context += ": "
|
| 15 |
+
context += df.iloc[i][j]
|
| 16 |
+
context += " "
|
| 17 |
+
context_data.append(context)
|
| 18 |
|
| 19 |
+
import os
|
| 20 |
|
| 21 |
+
# Get the secret key from the environment
|
| 22 |
+
groq_key = os.environ.get('gloq_key')
|
| 23 |
|
| 24 |
+
## LLM used for RAG
|
| 25 |
+
from langchain_groq import ChatGroq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
|
|
|
|
| 28 |
|
| 29 |
+
## Embedding model!
|
| 30 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 31 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 32 |
|
| 33 |
+
# create vector store!
|
| 34 |
+
from langchain_chroma import Chroma
|
| 35 |
+
|
| 36 |
+
vectorstore = Chroma(
|
| 37 |
+
collection_name="medical_dataset_store",
|
| 38 |
+
embedding_function=embed_model,
|
| 39 |
+
persist_directory="./",
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# add data to vector nstore
|
| 43 |
+
vectorstore.add_texts(context_data)
|
| 44 |
+
|
| 45 |
+
retriever = vectorstore.as_retriever()
|
| 46 |
+
|
| 47 |
+
from langchain_core.prompts import PromptTemplate
|
| 48 |
+
|
| 49 |
+
template = ("""You are a pharmacist and medical expert.
|
| 50 |
+
Use the provided context to answer the question.
|
| 51 |
+
If you don't know the answer, say so. Explain your answer in detail.
|
| 52 |
+
Do not discuss the context in your response; just provide the answer directly.
|
| 53 |
+
|
| 54 |
+
Context: {context}
|
| 55 |
+
|
| 56 |
+
Question: {question}
|
| 57 |
+
|
| 58 |
+
Answer:""")
|
| 59 |
+
|
| 60 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 61 |
+
|
| 62 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 63 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 64 |
+
|
| 65 |
+
rag_chain = (
|
| 66 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 67 |
+
| rag_prompt
|
| 68 |
+
| llm
|
| 69 |
+
| StrOutputParser()
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
import gradio as gr
|
| 74 |
+
|
| 75 |
+
# Function to stream responses
|
| 76 |
+
def rag_memory_stream(text):
|
| 77 |
+
partial_text = ""
|
| 78 |
+
for new_text in rag_chain.stream(text): # Assuming rag_chain is pre-defined
|
| 79 |
+
partial_text += new_text
|
| 80 |
+
yield partial_text
|
| 81 |
+
|
| 82 |
+
# Title and description for the app
|
| 83 |
+
title = "AI Medical Assistant for Drug Information and Side Effects"
|
| 84 |
+
description = """
|
| 85 |
+
This AI-powered chatbot is designed to provide reliable information about drugs, their side effects, and related medical conditions.
|
| 86 |
+
It utilizes the Groq API and LangChain to deliver real-time, accurate responses.
|
| 87 |
+
|
| 88 |
+
Ask questions like:
|
| 89 |
+
1. What are the side effects of taking aspirin daily?
|
| 90 |
+
2. What is the recommended treatment for a common cold?
|
| 91 |
+
3. What is the disease for constant fatigue and muscle weakness?
|
| 92 |
+
4. What are the symptoms of diabetes?
|
| 93 |
+
5. How can hypertension be managed?
|
| 94 |
+
|
| 95 |
+
**Disclaimer:** This chatbot is for informational purposes only and is not a substitute for professional medical advice.
|
| 96 |
"""
|
| 97 |
+
|
| 98 |
+
# Customizing Gradio interface for a better look
|
| 99 |
+
demo = gr.Interface(
|
| 100 |
+
fn=rag_memory_stream,
|
| 101 |
+
inputs=gr.Textbox(
|
| 102 |
+
lines=2,
|
| 103 |
+
placeholder="Type your medical question here...",
|
| 104 |
+
label="Your Medical Question"
|
| 105 |
+
),
|
| 106 |
+
outputs=gr.Textbox(
|
| 107 |
+
lines=10,
|
| 108 |
+
label="AI Response"
|
| 109 |
+
),
|
| 110 |
+
title=title,
|
| 111 |
+
description=description,
|
| 112 |
+
theme="compact", # Adding a compact theme for a polished look
|
| 113 |
+
allow_flagging="never"
|
| 114 |
)
|
| 115 |
|
| 116 |
+
# # Launching the app
|
| 117 |
+
# demo.launch(share=True)
|
| 118 |
+
|
| 119 |
+
# import gradio as gr
|
| 120 |
+
|
| 121 |
+
# def rag_memory_stream(text):
|
| 122 |
+
# partial_text = ""
|
| 123 |
+
# for new_text in rag_chain.stream(text):
|
| 124 |
+
# partial_text += new_text
|
| 125 |
+
# yield partial_text
|
| 126 |
+
|
| 127 |
+
# examples = ['I feel dizzy', 'what is the possible sickness for fatigue']
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
# title = "Real-time AI App with Groq API and LangChain to Answer medical questions"
|
| 133 |
+
# demo = gr.Interface(
|
| 134 |
+
# title=title,
|
| 135 |
+
# fn=rag_memory_stream,
|
| 136 |
+
# inputs="text",
|
| 137 |
+
# outputs="text",
|
| 138 |
+
# examples=examples,
|
| 139 |
+
# allow_flagging="never",
|
| 140 |
+
# )
|
| 141 |
+
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
+
demo.launch()
|