Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# Load pre-trained Sentence Transformer model
|
| 10 |
+
model = SentenceTransformer('LaBSE')
|
| 11 |
+
|
| 12 |
+
# Load questions and answers from the CSV file
|
| 13 |
+
df = pd.read_csv('combined_questions_and_answers.csv')
|
| 14 |
+
|
| 15 |
+
# Encode all questions in the dataset
|
| 16 |
+
question_embeddings = model.encode(df['Question'].tolist())
|
| 17 |
+
|
| 18 |
+
# Groq API setup
|
| 19 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 20 |
+
|
| 21 |
+
def llama_query(prompt, system_content):
|
| 22 |
+
response = client.chat.completions.create(
|
| 23 |
+
messages=[
|
| 24 |
+
{"role": "system", "content": system_content},
|
| 25 |
+
{"role": "user", "content": prompt}
|
| 26 |
+
],
|
| 27 |
+
model="llama3-70b-8192",
|
| 28 |
+
max_tokens=800,
|
| 29 |
+
temperature=0.7
|
| 30 |
+
)
|
| 31 |
+
return response.choices[0].message.content
|
| 32 |
+
|
| 33 |
+
def check_blood_donation_relevance(question):
|
| 34 |
+
prompt = f"Is the following question related to blood donation? Answer with 'Yes' or 'No': {question}"
|
| 35 |
+
system_content = "You are an assistant that determines if a question is related to blood donation."
|
| 36 |
+
response = llama_query(prompt, system_content)
|
| 37 |
+
return response.strip().lower() == 'yes'
|
| 38 |
+
|
| 39 |
+
def detect_language(text):
|
| 40 |
+
prompt = f"Detect the language of this text. If it's Swahili, return 'Swahili'. If it's English, return 'English'. Here's the text: {text}"
|
| 41 |
+
system_content = "You are a language detection assistant."
|
| 42 |
+
response = llama_query(prompt, system_content)
|
| 43 |
+
return response.strip().lower()
|
| 44 |
+
|
| 45 |
+
def translate_to_english(text):
|
| 46 |
+
prompt = f"Translate the following Swahili text to English: {text}"
|
| 47 |
+
system_content = "You are a translation assistant that translates from Swahili to English."
|
| 48 |
+
response = llama_query(prompt, system_content)
|
| 49 |
+
return response
|
| 50 |
+
|
| 51 |
+
def translate_to_swahili(text):
|
| 52 |
+
prompt = f"Translate the following text to simple Swahili, avoiding difficult words: {text}"
|
| 53 |
+
system_content = "You are a translation assistant that translates to simple Swahili."
|
| 54 |
+
response = llama_query(prompt, system_content)
|
| 55 |
+
return response
|
| 56 |
+
|
| 57 |
+
def refine_answer(question, retrieved_answer):
|
| 58 |
+
prompt = f"Question: {question}\nRetrieved Answer: {retrieved_answer}\nPlease refine the retrieved answer according to the question asked, ensuring it's clear and concise."
|
| 59 |
+
system_content = "You are an assistant that refines answers to make them more relevant and natural."
|
| 60 |
+
return llama_query(prompt, system_content)
|
| 61 |
+
|
| 62 |
+
def get_answer(user_question, threshold=0.35):
|
| 63 |
+
if not check_blood_donation_relevance(user_question):
|
| 64 |
+
return "I'm sorry, but your question doesn't seem to be related to blood donation. Could you please ask a question about blood donation?", 0
|
| 65 |
+
|
| 66 |
+
language = detect_language(user_question)
|
| 67 |
+
|
| 68 |
+
if language == 'swahili':
|
| 69 |
+
english_question = translate_to_english(user_question)
|
| 70 |
+
else:
|
| 71 |
+
english_question = user_question
|
| 72 |
+
|
| 73 |
+
user_embedding = model.encode(english_question)
|
| 74 |
+
|
| 75 |
+
similarities = cosine_similarity([user_embedding], question_embeddings)
|
| 76 |
+
|
| 77 |
+
max_similarity = np.max(similarities)
|
| 78 |
+
|
| 79 |
+
if max_similarity > threshold:
|
| 80 |
+
similar_question_idx = np.argmax(similarities)
|
| 81 |
+
retrieved_answer = df.iloc[similar_question_idx]['Answer']
|
| 82 |
+
refined_answer = refine_answer(english_question, retrieved_answer)
|
| 83 |
+
|
| 84 |
+
if language == 'swahili':
|
| 85 |
+
refined_answer = translate_to_swahili(refined_answer)
|
| 86 |
+
|
| 87 |
+
return refined_answer, max_similarity
|
| 88 |
+
else:
|
| 89 |
+
default_message = "The system couldn't find a sufficient answer to your question. Do you want to learn anything else about blood donation?"
|
| 90 |
+
if language == 'swahili':
|
| 91 |
+
default_message = translate_to_swahili(default_message)
|
| 92 |
+
return default_message, max_similarity
|
| 93 |
+
|
| 94 |
+
# Gradio app
|
| 95 |
+
def gradio_app(user_question):
|
| 96 |
+
answer, similarity = get_answer(user_question)
|
| 97 |
+
return f"Similarity: {similarity:.2f}\nAnswer: {answer}"
|
| 98 |
+
|
| 99 |
+
# Launch the Gradio app
|
| 100 |
+
iface = gr.Interface(
|
| 101 |
+
fn=gradio_app,
|
| 102 |
+
inputs=gr.Textbox(label="Enter your question"),
|
| 103 |
+
outputs=gr.Textbox(label="Answer"),
|
| 104 |
+
title="Blood Donation Q&A",
|
| 105 |
+
description="Ask questions related to blood donation and get answers in English or Swahili.",
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
iface.launch()
|