Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,98 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
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 |
-
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
42 |
|
43 |
-
""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
!kaggle datasets download fatemehmehrparvar/obesity-levels
|
3 |
+
!unzip obesity-levels
|
4 |
+
import pandas as pd
|
5 |
+
df = pd.read_csv('ObesityDataSet_raw_and_data_sinthetic.csv')
|
6 |
+
df.head()
|
7 |
+
df.columns
|
8 |
+
context_data=[]
|
9 |
+
for i in range(len(df)):
|
10 |
+
context = ""
|
11 |
+
for j in range(len(df.columns)):
|
12 |
+
context += df.columns[j]
|
13 |
+
context += ": "
|
14 |
+
context += str(df.iloc[i][j]) # Convert the value to string
|
15 |
+
context += " "
|
16 |
+
context_data.append(context)
|
17 |
+
len(context_data)
|
18 |
+
context_data[0]
|
19 |
+
from google.colab import userdata
|
20 |
+
groq_api_key = userdata.get('leoncie')
|
21 |
+
!pip install langchain_groq langchain_huggingface langchain_chroma
|
22 |
+
## LLM used for RAG
|
23 |
+
from langchain_groq import ChatGroq
|
24 |
|
25 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile",api_key=groq_api_key)
|
26 |
+
## Embedding model!
|
27 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
28 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
29 |
+
# create vector store!
|
30 |
+
from langchain_chroma import Chroma
|
31 |
|
32 |
+
vectorstore = Chroma(
|
33 |
+
collection_name="medical_dataset_store",
|
34 |
+
embedding_function=embed_model,
|
35 |
+
persist_directory="./",
|
36 |
+
)
|
37 |
+
vectorstore.get().keys()
|
38 |
+
# Limit the context_data to 100 entries
|
39 |
+
context_data_limited = context_data[:100]
|
40 |
|
41 |
+
# Add the limited context data to the vector store
|
42 |
+
vectorstore.add_texts(context_data_limited)
|
43 |
+
query = "What is the age of female who does not smoke and has weight of 64?"
|
44 |
+
docs = vectorstore.similarity_search(query)
|
45 |
+
print(docs[0].page_content)
|
46 |
+
retriever = vectorstore.as_retriever()
|
47 |
+
from langchain_core.prompts import PromptTemplate
|
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 |
+
from langchain_core.output_parsers import StrOutputParser
|
61 |
+
from langchain_core.runnables import RunnablePassthrough
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
rag_chain = (
|
64 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
65 |
+
| rag_prompt
|
66 |
+
| llm
|
67 |
+
| StrOutputParser()
|
68 |
+
)
|
69 |
+
from IPython.display import display, Markdown
|
70 |
|
71 |
+
response = rag_chain.invoke("What is the sex of a person whose age is 21 and has family overweight issue? ")
|
72 |
+
Markdown(response)
|
73 |
+
from IPython.display import display, Markdown
|
74 |
|
75 |
+
response = rag_chain.invoke("What is the minimum age of a person who does not smoke?")
|
76 |
+
Markdown(response)
|
77 |
+
!pip install gradio
|
78 |
+
import gradio as gr
|
79 |
+
|
80 |
+
def rag_memory_stream(text):
|
81 |
+
partial_text = ""
|
82 |
+
for new_text in rag_chain.stream(text):
|
83 |
+
partial_text += new_text
|
84 |
+
yield partial_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
|
87 |
+
title = "Real-time AI App with Groq API and LangChain to Answer Obsertity questions"
|
88 |
+
demo = gr.Interface(
|
89 |
+
title=title,
|
90 |
+
fn=rag_memory_stream,
|
91 |
+
inputs="text",
|
92 |
+
outputs="text",
|
93 |
+
allow_flagging="never",
|
94 |
+
)
|
95 |
+
|
96 |
+
demo.launch(share=True)
|
97 |
if __name__ == "__main__":
|
98 |
demo.launch()
|