Add application file
Browse files- app.py +52 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
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("/kaggle/working/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 |
+
iface = gr.Interface(fn=chatbot,
|
| 46 |
+
inputs="text",
|
| 47 |
+
outputs="text",
|
| 48 |
+
title="Medicine Chatbot",
|
| 49 |
+
description="Ask me about any medicine and get relevant information.")
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
faiss-cpu
|
| 3 |
+
sentence-transformers
|
| 4 |
+
requests
|
| 5 |
+
groq
|