Trip_Safely_1 / app.py
SageWisp's picture
Create app.py
eb53c50 verified
import gradio as gr
import pandas as pd
import json
from together import Together
# Load CSV data
prescription_meds_df = pd.read_csv('data/prescription_meds.csv')
psychedelics_df = pd.read_csv('data/psychedelics.csv')
interactions_df = pd.read_csv('data/interactions.csv')
# Initialize Together client
with open('configs.json', 'r') as f:
config = json.load(f)
TOGETHER_API_KEY = config["together"]
client = Together(api_key=TOGETHER_API_KEY)
def get_ai_safety_info(prescription, psychedelic, interaction_text):
prompt = f"""
As a harm reduction expert, please analyze this drug interaction and provide additional safety information:
{interaction_text}
Please include:
1. Additional information on drug interactions
2. Emergency warning signs to watch for
Keep the response factual and focused on harm reduction.
"""
response = client.chat.completions.create(
model="meta-llama/Meta-Llama-3-8B-Instruct-Lite",
messages=[{"role": "user", "content": prompt}],
)
return response.choices[0].message.content
def analyze_interaction(prescriptions, psychedelic):
if not prescriptions or not psychedelic:
return "Please select both prescription medication(s) and a psychedelic.", ""
result_text = ""
# Analyze each prescription
for prescription in prescriptions:
# Get drug information from DataFrames
med_info = prescription_meds_df[prescription_meds_df['name'] == prescription].iloc[0]
psych_info = psychedelics_df[psychedelics_df['name'] == psychedelic].iloc[0]
interaction = interactions_df[
(interactions_df['medication'] == prescription) &
(interactions_df['psychedelic'] == psychedelic)
].iloc[0]
# Format the results
result_text += f"""
PRESCRIPTION MEDICATION: {prescription}
Type: {med_info['type']}
Description: {med_info['description']}
Half-life: {med_info['half_life']}
INTERACTION ANALYSIS:
Risk Level: {interaction['risk_level']}
Description: {interaction['description']}
RECOMMENDATION:
{interaction['recommendation']}
-------------------
"""
# Add psychedelic information once
psych_info = psychedelics_df[psychedelics_df['name'] == psychedelic].iloc[0]
result_text += f"""
PSYCHEDELIC: {psychedelic}
Description: {psych_info['description']}
Duration: {psych_info['duration']}
Mechanisms: {psych_info['mechanisms']}
"""
return result_text
def get_safety_analysis(interaction_text):
if not interaction_text or interaction_text.startswith("Please select both"):
return "Please analyze drug interactions first before requesting AI safety information."
return get_ai_safety_info(None, None, interaction_text)
def create_interface():
# Get drug lists from DataFrames
prescription_meds = prescription_meds_df['name'].tolist()
psychedelics = psychedelics_df['name'].tolist()
with gr.Blocks(title="TripSafely - Drug Interaction Analyzer", theme=gr.themes.Soft()) as interface:
gr.Markdown("# TripSafely - Drug Interaction Analyzer")
gr.Markdown("Psychedelic & Prescription Drug Interaction Analyzer with AI Safety Information")
with gr.Row():
prescriptions = gr.CheckboxGroup(
choices=prescription_meds,
label="Select Prescription Medication(s)",
info="Select one or more medications"
)
psychedelic = gr.Radio(
choices=psychedelics,
label="Select Psychedelic",
)
analyze_btn = gr.Button("Analyze Interactions")
interaction_output = gr.Textbox(
label="Interaction Analysis",
lines=15,
)
safety_btn = gr.Button("Get AI Safety Analysis")
safety_output = gr.Textbox(
label="AI Safety Analysis",
lines=15,
)
gr.Markdown("*Disclaimer: This information is for educational purposes only and should not replace medical advice. AI-generated content should be verified with medical professionals.*")
analyze_btn.click(
fn=analyze_interaction,
inputs=[prescriptions, psychedelic],
outputs=interaction_output
)
safety_btn.click(
fn=get_safety_analysis,
inputs=[interaction_output],
outputs=safety_output
)
return interface
if __name__ == "__main__":
interface = create_interface()
interface.launch()