Spaces:
Sleeping
Sleeping
amirmmahdavikia
commited on
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Load the packages and configurations
|
2 |
+
import torch
|
3 |
+
import pandas as pd
|
4 |
+
from retriever import BM25Plus
|
5 |
+
import os
|
6 |
+
import streamlit as st
|
7 |
+
from groq import Groq
|
8 |
+
|
9 |
+
# Configure GROQ API_KEY
|
10 |
+
client = Groq(
|
11 |
+
api_key="GROQ_API_KEY",
|
12 |
+
)
|
13 |
+
|
14 |
+
# RTL formatting of the streamlit
|
15 |
+
st.markdown("""
|
16 |
+
<style>
|
17 |
+
body, html {
|
18 |
+
direction: RTL;
|
19 |
+
unicode-bidi: plaintext;
|
20 |
+
text-align: right;
|
21 |
+
}
|
22 |
+
p, div, input, label, h1, h2, h3, h4, h5, h6 {
|
23 |
+
direction: RTL;
|
24 |
+
unicode-bidi: plaintext;
|
25 |
+
text-align: right;
|
26 |
+
}
|
27 |
+
</style>
|
28 |
+
""", unsafe_allow_html=True)
|
29 |
+
|
30 |
+
# Streamlit details
|
31 |
+
st.title("DarooGAP")
|
32 |
+
st.sidebar.title("💊 داروگپ 💊")
|
33 |
+
st.sidebar.divider()
|
34 |
+
|
35 |
+
# Load the dataset
|
36 |
+
df = pd.read_csv('darooyab_qa.csv')
|
37 |
+
corpus = df.loc[:, 'Corpus'].to_list()
|
38 |
+
|
39 |
+
|
40 |
+
# Setup the BM25 retriever
|
41 |
+
def get_prompt(query, query_bm25):
|
42 |
+
prompt = 'به سوال کاربر بر اساس متن هایی که در ادامه آمده است پاسخ بدهید' + '\n'
|
43 |
+
prompt += 'اگر قادر به جواب دادن به سوال نبودی، عبارت زیر را خروجی بده:' + '\n'
|
44 |
+
prompt += 'متاسفم در حال حاضر اطلاعات زیادی درباره سوال شما نمی دانم!' + '\n\n'
|
45 |
+
prompt += f'سوال:' + '\n' + query + '\n\n'
|
46 |
+
for idx, topic in enumerate(query_bm25):
|
47 |
+
prompt += f'متن {idx+1}: ' + topic + '\n'
|
48 |
+
|
49 |
+
return prompt
|
50 |
+
|
51 |
+
|
52 |
+
def get_relevant_topics(query, corpus, n=3):
|
53 |
+
# Build the best match 25 base
|
54 |
+
tokenized_corpus = [doc.split(" ") for doc in corpus]
|
55 |
+
bm25 = BM25Plus(tokenized_corpus)
|
56 |
+
|
57 |
+
tokenized_query = query.split(" ")
|
58 |
+
query_bm = bm25.get_top_n(tokenized_query, corpus, n=n)
|
59 |
+
|
60 |
+
return query_bm
|
61 |
+
|
62 |
+
# Chatbot formatting
|
63 |
+
if "messages" not in st.session_state:
|
64 |
+
st.session_state["messages"] = [{"role": "assistant", "content": "چطور میتوانم به شما کمک کنم؟"}]
|
65 |
+
|
66 |
+
if "drugs" not in st.session_state:
|
67 |
+
st.session_state["drugs"] = {"drug_name": [], "drug_link": []}
|
68 |
+
|
69 |
+
for msg in st.session_state.messages:
|
70 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
71 |
+
|
72 |
+
|
73 |
+
if prompt := st.chat_input():
|
74 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
75 |
+
st.chat_message("user").write(prompt)
|
76 |
+
topics = get_relevant_topics(prompt, corpus, n=5)
|
77 |
+
relevant_drugs = df.iloc[list(topics.values())]
|
78 |
+
st.sidebar.header('داروها')
|
79 |
+
for drug in relevant_drugs.iterrows():
|
80 |
+
drug_name = drug[1]['Farsi_generic_name']
|
81 |
+
drug_link = drug[1]['Link']
|
82 |
+
if drug_name not in st.session_state.drugs['drug_name']:
|
83 |
+
st.session_state.drugs['drug_name'].append(drug_name)
|
84 |
+
st.session_state.drugs['drug_link'].append(drug_link)
|
85 |
+
|
86 |
+
for i in range(len(st.session_state.drugs['drug_name'])):
|
87 |
+
if st.session_state.drugs['drug_name'][i] in prompt:
|
88 |
+
st.sidebar.link_button(st.session_state.drugs['drug_name'][i], st.session_state.drugs['drug_link'][i])
|
89 |
+
|
90 |
+
prompt = get_prompt(prompt, topics)
|
91 |
+
response = client.chat.completions.create(
|
92 |
+
#
|
93 |
+
# Required parameters
|
94 |
+
#
|
95 |
+
messages=[
|
96 |
+
# Set an optional system message. This sets the behavior of the
|
97 |
+
# assistant and can be used to provide specific instructions for
|
98 |
+
# how it should behave throughout the conversation.
|
99 |
+
{
|
100 |
+
"role": "system",
|
101 |
+
"content": "تو یک دستیار سودمند هستی."
|
102 |
+
},
|
103 |
+
# Set a user message for the assistant to respond to.
|
104 |
+
{
|
105 |
+
"role": "user",
|
106 |
+
"content": prompt,
|
107 |
+
}
|
108 |
+
],
|
109 |
+
|
110 |
+
# The language model which will generate the completion.
|
111 |
+
model="llama3-70b-8192",
|
112 |
+
#
|
113 |
+
# Optional parameters
|
114 |
+
)
|
115 |
+
msg = response.choices[0].message.content
|
116 |
+
st.session_state.messages.append({"role": "assistant", "content": msg})
|
117 |
+
st.chat_message("assistant").write(msg)
|