GeneticDisorder / app.py
asadAbdullah's picture
Update app.py
7ed7e20 verified
raw
history blame
5.14 kB
# Import required libraries
import os
import pandas as pd
import streamlit as st
from transformers import pipeline
from sentence_transformers import SentenceTransformer, util
import requests
import json
# Configure Hugging Face API token securely
api_key = os.getenv("HF_API_KEY")
# Load the CSV dataset
try:
data = pd.read_csv('genetic-Final.csv')
except FileNotFoundError:
st.error("Dataset file not found. Please upload it to this directory.")
# Initialize Sentence Transformer model for RAG-based retrieval
retriever_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
# Preprocess the dataset for embeddings
if 'combined_description' not in data.columns:
data['combined_description'] = (
data['Symptoms'].fillna('') + " " +
data['Severity Level'].fillna('') + " " +
data['Risk Assessment'].fillna('') + " " +
data['Treatment Options'].fillna('') + " " +
data['Suggested Medical Tests'].fillna('') + " " +
data['Minimum Values for Medical Tests'].fillna('') + " " +
data['Emergency Treatment'].fillna('')
)
# Generate embeddings for the combined description if not already done
if 'embeddings' not in data.columns:
data['embeddings'] = data['combined_description'].apply(lambda x: retriever_model.encode(x).tolist())
# Function to retrieve relevant information based on user query
def get_relevant_info(query, top_k=3):
query_embedding = retriever_model.encode(query)
similarities = [util.cos_sim(query_embedding, doc_emb)[0][0].item() for doc_emb in data['embeddings']]
top_indices = sorted(range(len(similarities)), key=lambda i: similarities[i], reverse=True)[:top_k]
return data.iloc[top_indices]
# Function to generate response using Hugging Face Model API
# def generate_response(input_text, relevant_info):
# # Concatenate the relevant information as context for the model
# context = "\n".join(relevant_info['combined_description'].tolist())
# input_with_context = f"Context: {context}\n\nUser Query: {input_text}"
# api_url = "https://api-inference.huggingface.co/models/m42-health/Llama3-Med42-8B"
# headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACEHUB_API_TOKEN')}"}
# payload = {"inputs": input_with_context}
# try:
# response = requests.post(api_url, headers=headers, json=payload)
# response_data = response.json()
# if isinstance(response_data, list) and "generated_text" in response_data[0]:
# return response_data[0]["generated_text"]
# else:
# return "Unexpected response format from API."
# except Exception as e:
# st.error(f"Error during API request: {e}")
# return "Error processing your request."
def generate_response(input_text, relevant_info):
# Concatenate the relevant information as context for the model
context = "\n".join(relevant_info['combined_description'].tolist())
input_with_context = f"Context: {context}\n\nUser Query: {input_text}"
api_url = "https://api-inference.huggingface.co/models/m42-health/Llama3-Med42-8B"
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACEHUB_API_TOKEN')}"}
payload = {"inputs": input_with_context}
try:
response = requests.post(api_url, headers=headers, json=payload)
response_data = response.json()
# Print or display the raw response data
st.write("Raw API response:", response_data)
# Check and parse the response
if isinstance(response_data, list) and "generated_text" in response_data[0]:
return response_data[0]["generated_text"]
else:
return "Unexpected response format from API."
except Exception as e:
st.error(f"Error during API request: {e}")
return "Error processing your request."
# Check and parse the response if it's a single JSON object
if isinstance(response_data, dict) and "generated_text" in response_data:
return response_data["generated_text"]
# Streamlit UI for the FAQ Chatbot
def main():
st.title("Medical FAQ Chatbot")
st.sidebar.header("Ask a Question or Upload a Medical Report")
# Text input for user queries
user_query = st.sidebar.text_input("Type your medical question")
# File uploader for medical report (optional)
uploaded_file = st.sidebar.file_uploader("Upload a medical report (optional)", type=["txt", "pdf", "csv"])
# Process the query if provided
if user_query:
# Retrieve relevant information from dataset
relevant_info = get_relevant_info(user_query)
# Generate a combined FAQ-style response
faq_response = generate_response(user_query, relevant_info)
st.write("### FAQ Response:")
st.write(faq_response)
# Process the uploaded file if any
if uploaded_file:
# Placeholder for handling file analysis
st.write("### Uploaded Report Analysis:")
report_text = "Extracted report content here" # Placeholder for file processing logic
st.write(report_text)
if __name__ == "__main__":
main()