lyimo commited on
Commit
cdd279d
·
verified ·
1 Parent(s): e121fdb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
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('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
+ # Hugging Face API details for Meta-Llama 70B
19
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B"
20
+ headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
21
+
22
+ # Function to call Hugging Face API to refine and translate text
23
+ def refine_text(prompt):
24
+ payload = {
25
+ "inputs": prompt,
26
+ "parameters": {
27
+ "max_new_tokens": 800,
28
+ "temperature": 0.7
29
+ }
30
+ }
31
+ response = requests.post(API_URL, headers=headers, json=payload)
32
+ response_json = response.json()
33
+ if isinstance(response_json, list) and len(response_json) > 0:
34
+ return response_json[0].get('generated_text', '')
35
+ return "Error in refining text."
36
+
37
+ # Function to find the most similar question and provide the answer
38
+ def get_answer(user_question, threshold=0.30):
39
+ # Encode the user question
40
+ user_embedding = model.encode(user_question)
41
+
42
+ # Calculate cosine similarities
43
+ similarities = cosine_similarity([user_embedding], question_embeddings)
44
+
45
+ # Find the most similar question
46
+ max_similarity = np.max(similarities)
47
+ if max_similarity > threshold:
48
+ # Get the index of the most similar question
49
+ similar_question_idx = np.argmax(similarities)
50
+ # Retrieve the corresponding answer
51
+ answer = df.iloc[similar_question_idx]['Answer']
52
+ # Refine the answer using Meta-Llama 70B
53
+ refined_answer = refine_text(f"Refine this answer: {answer}")
54
+ return refined_answer, max_similarity
55
+ else:
56
+ return "The question appears to be out of domain. Kindly ask questions related to blood donations.", max_similarity
57
+
58
+ # Gradio app
59
+ def gradio_app(user_question):
60
+ answer, similarity = get_answer(user_question)
61
+ return f"Similarity: {similarity}\nAnswer: {answer}"
62
+
63
+ # Launch the Gradio app
64
+ iface = gr.Interface(
65
+ fn=gradio_app,
66
+ inputs=gr.Textbox(label="Enter your question"),
67
+ outputs=gr.Textbox(label="Answer"),
68
+ title="Blood Donation Q&A",
69
+ description="Ask questions related to blood donation and get answers.",
70
+ )
71
+
72
+ iface.launch()