File size: 2,554 Bytes
25e7f56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import openai
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
import pandas as pd
import gradio as gr

# Load pre-trained Sentence Transformer model
model = SentenceTransformer('all-MiniLM-L6-v2')

# Load questions and answers from the CSV file
df = pd.read_csv('combined_questions_and_answers.csv')

# Encode all questions in the dataset
question_embeddings = model.encode(df['Question'].tolist())

# OpenAI API key setup
openai.api_key = os.getenv("OPENAI_API_KEY")

# Function to call OpenAI API to refine and translate text
def refine_text(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"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."},
            {"role": "user", "content": prompt}
        ],
        max_tokens=800,
        n=1,
        stop=None,
        temperature=0.7
    )
    return response['choices'][0]['message']['content']

# Function to find the most similar question and provide the answer
def get_answer(user_question, threshold=0.80):
    # Encode the user question
    user_embedding = model.encode(user_question)

    # Calculate cosine similarities
    similarities = cosine_similarity([user_embedding], question_embeddings)

    # Find the most similar question
    max_similarity = np.max(similarities)
    if max_similarity > threshold:
        # Get the index of the most similar question
        similar_question_idx = np.argmax(similarities)
        # Retrieve the corresponding answer
        answer = df.iloc[similar_question_idx]['Answer']
        # Refine the answer using GPT-4
        refined_answer = refine_text(f"Refine this answer: {answer}")
        return refined_answer, max_similarity
    else:
        # Generate an answer using GPT-4 if no similar question is found
        refined_answer = refine_text(f"Answer this question: {user_question}")
        return refined_answer, max_similarity

# Gradio app
def gradio_app(user_question):
    answer, similarity = get_answer(user_question)
    return f"Similarity: {similarity}\nAnswer: {answer}"

# Launch the Gradio app
iface = gr.Interface(
    fn=gradio_app,
    inputs=gr.inputs.Textbox(label="Enter your question"),
    outputs=gr.outputs.Textbox(label="Answer"),
    title="Blood Donation Q&A",
    description="Ask questions related to blood donation and get answers.",
)

iface.launch()