Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
""
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 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 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
| 1 |
+
!kaggle datasets download adilmohammed/medical-data
|
| 2 |
+
!unzip medical-data
|
| 3 |
+
|
| 4 |
+
import pandas as pd
|
| 5 |
+
df = pd.read_csv('./medical_data.csv')
|
| 6 |
+
|
| 7 |
+
context_data = []
|
| 8 |
+
for i in range(len(df)):
|
| 9 |
+
context = ""
|
| 10 |
+
for j in range(3):
|
| 11 |
+
context += df.columns[j]
|
| 12 |
+
context += ": "
|
| 13 |
+
context += df.iloc[i][j]
|
| 14 |
+
context += " "
|
| 15 |
+
context_data.append(context)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
# Get the secret key from the environment
|
| 21 |
+
groq_key = os.environ.get('groq_api_keys')
|
| 22 |
+
|
| 23 |
+
## LLM used for RAG
|
| 24 |
+
from langchain_groq import ChatGroq
|
| 25 |
+
|
| 26 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_key)
|
| 27 |
+
|
| 28 |
+
## Embedding model!
|
| 29 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 30 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 31 |
+
|
| 32 |
+
# create vector store!
|
| 33 |
+
from langchain_chroma import Chroma
|
| 34 |
+
|
| 35 |
+
vectorstore = Chroma(
|
| 36 |
+
collection_name="medical_dataset_store",
|
| 37 |
+
embedding_function=embed_model,
|
| 38 |
+
persist_directory="./",
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# add data to vector nstore
|
| 42 |
+
vectorstore.add_texts(context_data)
|
| 43 |
+
|
| 44 |
+
retriever = vectorstore.as_retriever()
|
| 45 |
+
|
| 46 |
+
from langchain_core.prompts import PromptTemplate
|
| 47 |
+
|
| 48 |
+
template = ("""You are a medical expert.
|
| 49 |
+
Use the provided context to answer the question.
|
| 50 |
+
If you don't know the answer, say so. Explain your answer in detail.
|
| 51 |
+
Do not discuss the context in your response; just provide the answer directly.
|
| 52 |
+
|
| 53 |
+
Context: {context}
|
| 54 |
+
|
| 55 |
+
Question: {question}
|
| 56 |
+
|
| 57 |
+
Answer:""")
|
| 58 |
+
|
| 59 |
+
rag_prompt = PromptTemplate.from_template(template)
|
| 60 |
+
|
| 61 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 62 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 63 |
+
|
| 64 |
+
rag_chain = (
|
| 65 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
| 66 |
+
| rag_prompt
|
| 67 |
+
| llm
|
| 68 |
+
| StrOutputParser()
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
import gradio as gr
|
| 72 |
+
|
| 73 |
+
def rag_memory_stream(text):
|
| 74 |
+
partial_text = ""
|
| 75 |
+
for new_text in rag_chain.stream(text):
|
| 76 |
+
partial_text += new_text
|
| 77 |
+
yield partial_text
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
title = "Real-time AI App with Groq API and LangChain to Answer medical questions"
|
| 81 |
+
demo = gr.Interface(
|
| 82 |
+
title=title,
|
| 83 |
+
fn=rag_memory_stream,
|
| 84 |
+
inputs="text",
|
| 85 |
+
outputs="text",
|
| 86 |
+
allow_flagging="never",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
|