sharmapacific commited on
Commit
f306ba2
·
1 Parent(s): 06f0ee6

added the complete module

Browse files
Files changed (2) hide show
  1. app.py +173 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import google.generativeai as genai
2
+ from langchain.prompts import PromptTemplate
3
+ import gradio as gr
4
+ import pdfplumber
5
+ import os
6
+ from dotenv import load_dotenv
7
+
8
+ # Load environment variables from .env file
9
+ load_dotenv()
10
+
11
+ # Configure the API key
12
+ api_key = os.getenv('API_KEY')
13
+
14
+ # Configure the API key for Google's generative AI
15
+ genai.configure(api_key=api_key)
16
+
17
+ # Define the generative model
18
+ model = genai.GenerativeModel('gemini-pro')
19
+
20
+ # Function to extract text from a PDF file and generate a response
21
+ def retrieve_text_from_pdf(file_path):
22
+ with open(file_path, 'rb') as f:
23
+ pdf = pdfplumber.open(f)
24
+ text = ""
25
+ for page in pdf.pages:
26
+ text += page.extract_text()
27
+ return text
28
+
29
+ # Function to analyze the text and generate a response
30
+ def process_text(text):
31
+ prompt_template = PromptTemplate.from_template("""
32
+ You are a virtual doctor. A patient has provided their medical report, and you need to assist them with their health journey.
33
+
34
+ **Patient Instructions:**
35
+ - Analyze the medical report.
36
+ - Identify the patient's health issues.
37
+ - Provide a comprehensive solution including:
38
+ - Diagnosis of the problem.
39
+ - Recommended treatments or remedies.
40
+ - Suggested physical activities.
41
+ - Habits to give up.
42
+ - Fruits and foods to include in the diet.
43
+ - Ensure the guidance is clear and actionable.
44
+
45
+ **Medical Report:**
46
+ ```
47
+ {PDF_text}
48
+ ```
49
+ **Solution:**
50
+ ```
51
+ [Provide detailed analysis and recommendations here]
52
+ ```"""
53
+ )
54
+ prompt = prompt_template.format(PDF_text=text)
55
+ response = model.generate_content(prompt).text
56
+ return response, text
57
+ # formatted_text = f"Paste this text in the diet plan and exercise plan sections to generate the plans:\n\n{text}"
58
+ # formatted_response = f"Report Explanations:\n\n{response}"
59
+ # return formatted_response, formatted_text
60
+
61
+ # Function to handle both PDF upload and text input
62
+ def process_user_input(input_type, pdf_file=None, report_text=None):
63
+ if input_type == "Upload PDF" and pdf_file is not None:
64
+ text = retrieve_text_from_pdf(pdf_file.name)
65
+ elif input_type == "Paste Text" and report_text is not None:
66
+ text = report_text
67
+ else:
68
+ return "Please provide a valid input.", ""
69
+
70
+ return process_text(text)
71
+
72
+ # Function to check symptoms
73
+ def assess_symptoms(symptoms):
74
+ prompt = f"As a virtual doctor, please analyze the following symptoms and provide potential conditions:\n{symptoms}"
75
+ response = model.generate_content(prompt).text
76
+ return response
77
+
78
+ # Function to generate a diet plan based on the medical report
79
+ def develop_diet_plan(report_text):
80
+ prompt = f"Based on the medical report, provide a Customized diet and nutrition plan:\n{report_text}"
81
+ response = model.generate_content(prompt).text
82
+ return response
83
+
84
+ # Function to generate an exercise plan based on the medical report
85
+ def develop_exercise_plan(report_text):
86
+ prompt = f"Based on the medical report, provide a Customized exercise plan:\n{report_text}"
87
+ response = model.generate_content(prompt).text
88
+ return response
89
+
90
+ # Function to set medication reminders
91
+ def schedule_medication_reminder(medication, time):
92
+ return f"Reminder set for {medication} at {time}."
93
+
94
+ # Function to provide health education content
95
+ def develop_health_content(topic):
96
+ prompt = f"Provide educational content on the following health topic:\n{topic}"
97
+ response = model.generate_content(prompt).text
98
+ return response
99
+
100
+ # Define Gradio interfaces for each function
101
+ demo = gr.Interface(
102
+ fn=process_user_input,
103
+ inputs=[
104
+ gr.Radio(["Upload PDF", "Paste Text"], label="Select Input Method", value="Upload PDF"),
105
+ gr.File(label="Upload PDF"),
106
+ gr.Textbox(label="Or Paste Report Text here", lines=10)
107
+ ],
108
+ outputs=[gr.Textbox(label="Report Analysis"), gr.Textbox(label="Copy and Paste this Medical Report Text in Diet/Exercise Plan Generation")],
109
+ description="Discover Your Path to Well-being",
110
+ )
111
+
112
+ present_symptom_checker = gr.Interface(
113
+ fn=assess_symptoms,
114
+ inputs=gr.Textbox(placeholder="Enter your symptoms, e.g., fever, cough, fatigue", lines=2),
115
+ outputs=gr.Textbox(label="Potential Conditions"),
116
+ description="Symptom Checker",
117
+ examples=[
118
+ ["fever, cough, fatigue"],
119
+ ["headache, nausea, dizziness"]
120
+ ]
121
+ )
122
+
123
+ present_diet_plan = gr.Interface(
124
+ fn=develop_diet_plan,
125
+ inputs=gr.Textbox(placeholder="Paste the copied medical report text here", lines=10),
126
+ outputs=gr.Textbox(label="Customized Diet Plan"),
127
+ description="Customized Diet Plan",
128
+ )
129
+
130
+ present_exercise_plan = gr.Interface(
131
+ fn=develop_exercise_plan,
132
+ inputs=gr.Textbox(placeholder="Paste the copied medical report text here", lines=10),
133
+ outputs=gr.Textbox(label="Customized Exercise Plan"),
134
+ description="Customized Exercise Plan",
135
+ )
136
+
137
+ present_medication_reminder = gr.Interface(
138
+ fn=schedule_medication_reminder,
139
+ inputs=[gr.Textbox(placeholder="Enter medication name", lines=1), gr.Textbox(placeholder="Enter time, e.g., 10:00 AM", lines=1)],
140
+ outputs=gr.Textbox(label="Medication Reminder"),
141
+ description="Set Medication Reminder",
142
+ )
143
+
144
+ present_health_education = gr.Interface(
145
+ fn=develop_health_content,
146
+ inputs=gr.Textbox(placeholder="Enter health topic, e.g., benefits of regular exercise", lines=2),
147
+ outputs=gr.Textbox(label="Learning materials"),
148
+ description="Health Education",
149
+ examples=[
150
+ ["benefits of regular exercise"]
151
+ ]
152
+ )
153
+
154
+
155
+ # Define the title and tabbed interface
156
+ title = gr.Markdown("# AI-Powered Virtual Health Assistant")
157
+
158
+ tabs = gr.TabbedInterface(
159
+ [demo, present_symptom_checker, present_diet_plan, present_exercise_plan, present_medication_reminder, present_health_education],
160
+ ["Medical Report Analysis", "Symptom Checker", "Diet Plan", "Exercise Plan", "Medication Reminder", "Health Education"]
161
+ )
162
+
163
+ # Set up the layout
164
+ def create_interface():
165
+ with gr.Blocks() as app:
166
+ title.render() # Render the title
167
+ tabs.render() # Render the tabs
168
+ return app
169
+
170
+ # Launch the application
171
+ app = create_interface()
172
+ app.launch(debug=True)
173
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ google-generativeai==0.1.0
2
+ langchain==0.2.11
3
+ gradio==4.39.0
4
+ gradio_client==1.1.1
5
+ pdfplumber==0.11.2
6
+ python-dotenv