Leoncie commited on
Commit
a7c992a
·
verified ·
1 Parent(s): 6ee7acb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -50
app.py CHANGED
@@ -1,64 +1,98 @@
1
- import gradio as gr
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
- def respond(
11
- message,
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
- 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
 
 
 
 
 
 
 
 
 
 
 
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()