Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
+
from sentence_transformers import SentenceTransformer
|
4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
# Load pre-trained Sentence Transformer model
|
10 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
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 |
+
# OpenAI API key setup
|
19 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
20 |
+
|
21 |
+
# Function to call OpenAI API to refine and translate text
|
22 |
+
def refine_text(prompt):
|
23 |
+
response = openai.ChatCompletion.create(
|
24 |
+
model="gpt-4",
|
25 |
+
messages=[
|
26 |
+
{"role": "system", "content": "You are an assistant that refines text to make it conversational and natural. If the question is in Swahili, respond in Swahili."},
|
27 |
+
{"role": "user", "content": prompt}
|
28 |
+
],
|
29 |
+
max_tokens=800,
|
30 |
+
n=1,
|
31 |
+
stop=None,
|
32 |
+
temperature=0.7
|
33 |
+
)
|
34 |
+
return response['choices'][0]['message']['content']
|
35 |
+
|
36 |
+
# Function to find the most similar question and provide the answer
|
37 |
+
def get_answer(user_question, threshold=0.80):
|
38 |
+
# Encode the user question
|
39 |
+
user_embedding = model.encode(user_question)
|
40 |
+
|
41 |
+
# Calculate cosine similarities
|
42 |
+
similarities = cosine_similarity([user_embedding], question_embeddings)
|
43 |
+
|
44 |
+
# Find the most similar question
|
45 |
+
max_similarity = np.max(similarities)
|
46 |
+
if max_similarity > threshold:
|
47 |
+
# Get the index of the most similar question
|
48 |
+
similar_question_idx = np.argmax(similarities)
|
49 |
+
# Retrieve the corresponding answer
|
50 |
+
answer = df.iloc[similar_question_idx]['Answer']
|
51 |
+
# Refine the answer using GPT-4
|
52 |
+
refined_answer = refine_text(f"Refine this answer: {answer}")
|
53 |
+
return refined_answer, max_similarity
|
54 |
+
else:
|
55 |
+
# Generate an answer using GPT-4 if no similar question is found
|
56 |
+
refined_answer = refine_text(f"Answer this question: {user_question}")
|
57 |
+
return refined_answer, max_similarity
|
58 |
+
|
59 |
+
# Gradio app
|
60 |
+
def gradio_app(user_question):
|
61 |
+
answer, similarity = get_answer(user_question)
|
62 |
+
return f"Similarity: {similarity}\nAnswer: {answer}"
|
63 |
+
|
64 |
+
# Launch the Gradio app
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=gradio_app,
|
67 |
+
inputs=gr.inputs.Textbox(label="Enter your question"),
|
68 |
+
outputs=gr.outputs.Textbox(label="Answer"),
|
69 |
+
title="Blood Donation Q&A",
|
70 |
+
description="Ask questions related to blood donation and get answers.",
|
71 |
+
)
|
72 |
+
|
73 |
+
iface.launch()
|