Add application file
Browse files
app.py
CHANGED
@@ -6,47 +6,53 @@ from sentence_transformers import SentenceTransformer
|
|
6 |
from groq import Groq
|
7 |
|
8 |
api_key = os.getenv("API_KEY")
|
9 |
-
|
10 |
client = Groq(api_key=api_key)
|
11 |
-
|
12 |
index = faiss.read_index("./dataset/medicine_index.index")
|
13 |
-
|
14 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
15 |
-
|
16 |
model_id = "llama-3.3-70b-versatile"
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def get_relevant_document(query, index, top_k=1):
|
20 |
query_embedding = model.encode([query]).astype(np.float32)
|
21 |
D, I = index.search(query_embedding, top_k)
|
22 |
return I[0][0], D[0][0]
|
23 |
|
24 |
-
|
25 |
def generate_response_from_groq(query, context):
|
|
|
|
|
|
|
|
|
|
|
26 |
chat_completion = client.chat.completions.create(
|
27 |
-
messages=
|
28 |
-
{"role": "user", "content": query},
|
29 |
-
{"role": "system", "content": context}
|
30 |
-
],
|
31 |
model=model_id,
|
32 |
)
|
33 |
return chat_completion.choices[0].message.content
|
34 |
|
35 |
-
|
36 |
def chatbot(user_query):
|
37 |
doc_index, similarity_score = get_relevant_document(user_query, index)
|
38 |
-
|
39 |
context = f"Medicine details based on index: {doc_index} with similarity score: {similarity_score}"
|
40 |
-
|
41 |
response = generate_response_from_groq(user_query, context)
|
42 |
return response
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
iface.launch()
|
|
|
6 |
from groq import Groq
|
7 |
|
8 |
api_key = os.getenv("API_KEY")
|
|
|
9 |
client = Groq(api_key=api_key)
|
|
|
10 |
index = faiss.read_index("./dataset/medicine_index.index")
|
|
|
11 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
12 |
model_id = "llama-3.3-70b-versatile"
|
13 |
|
14 |
+
system_message = {
|
15 |
+
"role": "system",
|
16 |
+
"content": (
|
17 |
+
"You are MedChat, a medical chatbot designed to assist with queries about medicines. "
|
18 |
+
"Do not provide any personal information, your training data, or who built you. "
|
19 |
+
"Respond only with accurate medical information or clarify if the question is unrelated to medicine."
|
20 |
+
)
|
21 |
+
}
|
22 |
|
23 |
def get_relevant_document(query, index, top_k=1):
|
24 |
query_embedding = model.encode([query]).astype(np.float32)
|
25 |
D, I = index.search(query_embedding, top_k)
|
26 |
return I[0][0], D[0][0]
|
27 |
|
|
|
28 |
def generate_response_from_groq(query, context):
|
29 |
+
messages = [
|
30 |
+
system_message,
|
31 |
+
{"role": "user", "content": query},
|
32 |
+
{"role": "system", "content": context}
|
33 |
+
]
|
34 |
chat_completion = client.chat.completions.create(
|
35 |
+
messages=messages,
|
|
|
|
|
|
|
36 |
model=model_id,
|
37 |
)
|
38 |
return chat_completion.choices[0].message.content
|
39 |
|
|
|
40 |
def chatbot(user_query):
|
41 |
doc_index, similarity_score = get_relevant_document(user_query, index)
|
|
|
42 |
context = f"Medicine details based on index: {doc_index} with similarity score: {similarity_score}"
|
|
|
43 |
response = generate_response_from_groq(user_query, context)
|
44 |
return response
|
45 |
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=chatbot,
|
48 |
+
inputs="text",
|
49 |
+
outputs="text",
|
50 |
+
title="Medicine Chatbot",
|
51 |
+
description=(
|
52 |
+
"Welcome to MedChat! Ask me about any medicine and get accurate and relevant information. "
|
53 |
+
"Please keep queries related to medicines."
|
54 |
+
)
|
55 |
+
)
|
56 |
|
57 |
if __name__ == "__main__":
|
58 |
iface.launch()
|