Spaces:
Runtime error
Runtime error
| import os | |
| import requests | |
| 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('LaBSE') | |
| # 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()) | |
| # Hugging Face API details for Meta-Llama 70B | |
| API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-70B" | |
| headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"} | |
| # Function to call Hugging Face API to refine and translate text | |
| def refine_text(prompt): | |
| payload = { | |
| "inputs": prompt, | |
| "parameters": { | |
| "max_new_tokens": 800, | |
| "temperature": 0.7 | |
| } | |
| } | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| response_json = response.json() | |
| if isinstance(response_json, list) and len(response_json) > 0: | |
| return response_json[0].get('generated_text', '') | |
| return "Error in refining text." | |
| # Function to find the most similar question and provide the answer | |
| def get_answer(user_question, threshold=0.30): | |
| # 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 Meta-Llama 70B | |
| refined_answer = refine_text(f"Refine this answer: {answer}") | |
| return refined_answer, max_similarity | |
| else: | |
| return "The question appears to be out of domain. Kindly ask questions related to blood donations.", 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.Textbox(label="Enter your question"), | |
| outputs=gr.Textbox(label="Answer"), | |
| title="Blood Donation Q&A", | |
| description="Ask questions related to blood donation and get answers.", | |
| ) | |
| iface.launch() |