SageWisp commited on
Commit
eb53c50
·
verified ·
1 Parent(s): 56f6bb6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -0
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import json
4
+ from together import Together
5
+
6
+ # Load CSV data
7
+ prescription_meds_df = pd.read_csv('data/prescription_meds.csv')
8
+ psychedelics_df = pd.read_csv('data/psychedelics.csv')
9
+ interactions_df = pd.read_csv('data/interactions.csv')
10
+
11
+ # Initialize Together client
12
+ with open('configs.json', 'r') as f:
13
+ config = json.load(f)
14
+
15
+ TOGETHER_API_KEY = config["together"]
16
+ client = Together(api_key=TOGETHER_API_KEY)
17
+
18
+ def get_ai_safety_info(prescription, psychedelic, interaction_text):
19
+ prompt = f"""
20
+ As a harm reduction expert, please analyze this drug interaction and provide additional safety information:
21
+
22
+ {interaction_text}
23
+
24
+ Please include:
25
+ 1. Additional information on drug interactions
26
+ 2. Emergency warning signs to watch for
27
+
28
+ Keep the response factual and focused on harm reduction.
29
+ """
30
+
31
+ response = client.chat.completions.create(
32
+ model="meta-llama/Meta-Llama-3-8B-Instruct-Lite",
33
+ messages=[{"role": "user", "content": prompt}],
34
+ )
35
+ return response.choices[0].message.content
36
+
37
+ def analyze_interaction(prescriptions, psychedelic):
38
+ if not prescriptions or not psychedelic:
39
+ return "Please select both prescription medication(s) and a psychedelic.", ""
40
+
41
+ result_text = ""
42
+
43
+ # Analyze each prescription
44
+ for prescription in prescriptions:
45
+ # Get drug information from DataFrames
46
+ med_info = prescription_meds_df[prescription_meds_df['name'] == prescription].iloc[0]
47
+ psych_info = psychedelics_df[psychedelics_df['name'] == psychedelic].iloc[0]
48
+ interaction = interactions_df[
49
+ (interactions_df['medication'] == prescription) &
50
+ (interactions_df['psychedelic'] == psychedelic)
51
+ ].iloc[0]
52
+
53
+ # Format the results
54
+ result_text += f"""
55
+ PRESCRIPTION MEDICATION: {prescription}
56
+ Type: {med_info['type']}
57
+ Description: {med_info['description']}
58
+ Half-life: {med_info['half_life']}
59
+
60
+ INTERACTION ANALYSIS:
61
+ Risk Level: {interaction['risk_level']}
62
+ Description: {interaction['description']}
63
+
64
+ RECOMMENDATION:
65
+ {interaction['recommendation']}
66
+ -------------------
67
+ """
68
+
69
+ # Add psychedelic information once
70
+ psych_info = psychedelics_df[psychedelics_df['name'] == psychedelic].iloc[0]
71
+ result_text += f"""
72
+ PSYCHEDELIC: {psychedelic}
73
+ Description: {psych_info['description']}
74
+ Duration: {psych_info['duration']}
75
+ Mechanisms: {psych_info['mechanisms']}
76
+ """
77
+
78
+ return result_text
79
+
80
+ def get_safety_analysis(interaction_text):
81
+ if not interaction_text or interaction_text.startswith("Please select both"):
82
+ return "Please analyze drug interactions first before requesting AI safety information."
83
+
84
+ return get_ai_safety_info(None, None, interaction_text)
85
+
86
+ def create_interface():
87
+ # Get drug lists from DataFrames
88
+ prescription_meds = prescription_meds_df['name'].tolist()
89
+ psychedelics = psychedelics_df['name'].tolist()
90
+
91
+ with gr.Blocks(title="TripSafely - Drug Interaction Analyzer", theme=gr.themes.Soft()) as interface:
92
+ gr.Markdown("# TripSafely - Drug Interaction Analyzer")
93
+ gr.Markdown("Psychedelic & Prescription Drug Interaction Analyzer with AI Safety Information")
94
+
95
+ with gr.Row():
96
+ prescriptions = gr.CheckboxGroup(
97
+ choices=prescription_meds,
98
+ label="Select Prescription Medication(s)",
99
+ info="Select one or more medications"
100
+ )
101
+ psychedelic = gr.Radio(
102
+ choices=psychedelics,
103
+ label="Select Psychedelic",
104
+ )
105
+
106
+ analyze_btn = gr.Button("Analyze Interactions")
107
+ interaction_output = gr.Textbox(
108
+ label="Interaction Analysis",
109
+ lines=15,
110
+ )
111
+
112
+ safety_btn = gr.Button("Get AI Safety Analysis")
113
+ safety_output = gr.Textbox(
114
+ label="AI Safety Analysis",
115
+ lines=15,
116
+ )
117
+
118
+ 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.*")
119
+
120
+ analyze_btn.click(
121
+ fn=analyze_interaction,
122
+ inputs=[prescriptions, psychedelic],
123
+ outputs=interaction_output
124
+ )
125
+
126
+ safety_btn.click(
127
+ fn=get_safety_analysis,
128
+ inputs=[interaction_output],
129
+ outputs=safety_output
130
+ )
131
+
132
+ return interface
133
+
134
+ if __name__ == "__main__":
135
+ interface = create_interface()
136
+ interface.launch()