vinayakarsh commited on
Commit
259d0bd
·
verified ·
1 Parent(s): 9d186e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -23
app.py CHANGED
@@ -1,39 +1,63 @@
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
4
-
5
 
6
  GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
7
  genai.configure(api_key=GOOGLE_API_KEY)
8
  model = genai.GenerativeModel('gemini-2.0-flash-exp')
9
  generation_config = genai.GenerationConfig(temperature=0)
10
 
 
 
 
11
 
12
- def upload(file):
13
- if not file:
14
- return "**ERROR!!! FILE NOT UPLOADED**"
15
-
16
- prompt = "You are an expert in medical report analysis. Analyze the given medical report and generate a summarized report of the same."\
17
- "All the medical tests in the given medical report must be specified in the summarized report. The response generated should be well-structured with health tips based on the report."\
18
-
19
- file = genai.upload_file(file)
20
 
21
- contents = [prompt, file]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- response = model.generate_content(contents)
24
-
25
- file.delete()
 
 
26
 
27
- return response.text
28
-
29
-
30
- demo = gr.Interface(
31
- fn = upload,
32
- inputs = gr.File(file_types=['.pdf']), # Input: File input
33
- outputs = gr.Markdown(), # Output: Textbox to display the extracted text
34
- title = "Medical Report Summarizer",
35
- description = "Note: This summary is based on the provided medical report and does not constitute medical advice. Please consult with your healthcare provider for any questions or concerns regarding your health."
36
- )
 
 
 
 
 
 
37
 
38
 
39
  demo.launch()
 
1
  import os
2
  import google.generativeai as genai
3
  import gradio as gr
4
+ import time
5
 
6
  GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
7
  genai.configure(api_key=GOOGLE_API_KEY)
8
  model = genai.GenerativeModel('gemini-2.0-flash-exp')
9
  generation_config = genai.GenerationConfig(temperature=0)
10
 
11
+ input_file = None
12
+ prompt = prompt = "You are an expert in medical report analysis. Analyze the given medical report and generate a summarized report of the same."\
13
+ "All the medical tests in the given medical report must be specified in the summarized report. The response generated should be well-structured with health tips based on the report."
14
 
15
+ def greet(message, chatbot):
16
+ global input_file
17
+ if not input_file:
18
+ raise gr.Error(message="File not uploaded !!!", print_exception=False)
 
 
 
 
19
 
20
+ chatbot.append({"role": "user", "content": message})
21
+ yield chatbot
22
+
23
+ history = chatbot+["-1"]
24
+
25
+ global prompt, input_file
26
+ response = model.generate_content([prompt, input_file]).text
27
+ assistant_message = ""
28
+ for char in response:
29
+ assistant_message += char
30
+ history[-1] = {"role": "assistant", "content": assistant_message}
31
+ yield history
32
+ time.sleep(0.1)
33
+
34
+ with gr.Blocks() as demo:
35
+ def upload_file(file):
36
+ global input_file
37
+ input_file = genai.upload_file(file)
38
 
39
+ def clear_file():
40
+ global input_file
41
+ input_file.delete()
42
+
43
+ input_file = None
44
 
45
+ return [],""
46
+
47
+ with gr.Row():
48
+ with gr.Column(scale=5):
49
+ chatbot = gr.Chatbot(type="messages", render_markdown=True, height=550)
50
+ message = gr.Textbox(label="Enter your prompt")
51
+ submit = gr.Button("Submit")
52
+ clear = gr.ClearButton([message, chatbot])
53
+
54
+ submit.click(greet, [message, chatbot], chatbot)
55
+
56
+ with gr.Column(scale=1):
57
+ file_upload = gr.File(label="Upload PDF File", file_types=['.pdf', '.txt'], )
58
+
59
+ file_upload.upload(upload_file, file_upload)
60
+ file_upload.clear(clear_file, None, [chatbot, message])
61
 
62
 
63
  demo.launch()