import torch import gradio as gr import pandas as pd from transformers import AutoModelForSequenceClassification, AutoTokenizer import joblib import io, base64 import matplotlib.pyplot as plt # Load model and tokenizer from the Hub model = AutoModelForSequenceClassification.from_pretrained("modernbert-disease-classifier") tokenizer = AutoTokenizer.from_pretrained("modernbert-disease-classifier") # Load additional assets (drug recommendation data and label encoder) drug_df = pd.read_csv("drug_recommendation_data.csv") label_encoder = joblib.load("label_encoder.pkl") device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) def recommend_drugs(condition, df, top_n=3): condition_df = df[df['condition'] == condition] if condition_df.empty: return f"
No data available for condition: {condition}
" high_rated = condition_df[condition_df['rating'] >= 8] if high_rated.empty: return f"No high-rated drugs available for condition: {condition}
" drug_stats = high_rated.groupby('drugName').agg( num_reviews=('rating', 'size'), avg_rating=('rating', 'mean'), total_usefulness=('usefulCount', 'sum') ).reset_index() drug_stats = drug_stats.sort_values( by=['num_reviews', 'avg_rating', 'total_usefulness'], ascending=False ).head(top_n) colors = ["#e6194b", "#3cb44b", "#ffe119", "#4363d8", "#f58231"] recommendations = "Error: {str(e)}
" # (Optional) Add additional features such as chatbot or visualization here. # Create a Gradio interface interface = gr.Interface( fn=predict_and_recommend, inputs=gr.Textbox(label="Patient Review", placeholder="Enter medical review...", lines=4), outputs=gr.HTML(label="Results"), title="🩺 Disease Classifier & Drug Recommender", description="Enter a patient review to predict a medical condition and get drug recommendations.", theme="JohnSmith9982/small_and_pretty" ) if __name__ == "__main__": interface.launch()