Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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()
|