dindizz commited on
Commit
7d4fb18
·
verified ·
1 Parent(s): 0f7a806

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ import os
4
+ import PyPDF2
5
+ from docx import Document
6
+ from PIL import Image
7
+ import pytesseract
8
+ import json
9
+
10
+ # Load your OpenAI API key and prompt from environment variables
11
+ openai.api_key = os.getenv("HF_API_KEY_OPENAI")
12
+ preset_prompt = os.getenv("HF_PROMPT_HEART_HEALTH")
13
+
14
+ # Risk Disclaimer to be added at the end of the report
15
+ risk_disclaimer = f'''
16
+ Risk Disclaimer:
17
+ This AI-generated summary report provided is intended solely for use as a decision support tool and should not be considered as medical diagnosis. These reports are designed to assist individuals and healthcare professionals and should be used in conjunction with clinical judgment and other diagnostic methods. The AI system does not replace the expertise of qualified medical practitioners. Users should not make any healthcare decisions based solely on AI-generated reports without consulting with a licensed healthcare provider.
18
+ '''
19
+
20
+ # Function to extract text from a PDF
21
+ def extract_text_from_pdf(pdf_file):
22
+ reader = PyPDF2.PdfReader(pdf_file)
23
+ text = ""
24
+ for page in reader.pages:
25
+ text += page.extract_text()
26
+ return text
27
+
28
+ # Function to extract text from a DOCX file
29
+ def extract_text_from_docx(docx_file):
30
+ doc = Document(docx_file)
31
+ text = ""
32
+ for paragraph in doc.paragraphs:
33
+ text += paragraph.text
34
+ return text
35
+
36
+ # Function to extract text from an image using OCR
37
+ def extract_text_from_image(image_file):
38
+ image = Image.open(image_file)
39
+ text = pytesseract.image_to_string(image)
40
+ return text
41
+
42
+ # Function to detect file type and extract text accordingly
43
+ def extract_text(file):
44
+ file_type = file.name.split('.')[-1].lower()
45
+ if file_type == 'pdf':
46
+ return extract_text_from_pdf(file)
47
+ elif file_type == 'docx':
48
+ return extract_text_from_docx(file)
49
+ elif file_type in ['jpg', 'jpeg', 'png']:
50
+ return extract_text_from_image(file)
51
+ elif file_type == 'txt':
52
+ return file.read().decode('utf-8')
53
+ else:
54
+ return "Unsupported file format."
55
+
56
+ # Function to interact with the GPT-4o-mini model via the OpenAI API
57
+ def heart_health_bot(file):
58
+ if file is None:
59
+ return "Please upload a PDF, DOCX or text file for analysis."
60
+
61
+ context = extract_text(file)
62
+ if "Unsupported file format" in context:
63
+ return context
64
+
65
+ # Use the secret prompt directly in the API call
66
+ response = openai.chat.completions.create(
67
+ model="gpt-4o-mini",
68
+ messages=[
69
+ {"role": "system", "content": preset_prompt},
70
+ {"role": "user", "content": context}
71
+ ],
72
+ max_tokens=3000, # Increased to capture the complete analysis
73
+ temperature=0.7,
74
+ )
75
+
76
+ # Extract the response text and remove any leading/trailing whitespace
77
+ result = response.choices[0].message.content.strip()
78
+
79
+ # Append the risk disclaimer to the result
80
+ result_with_disclaimer = f"{result}\n\n{risk_disclaimer}"
81
+
82
+ # Create a JSON output
83
+ output_json = {
84
+ "result": result_with_disclaimer
85
+ }
86
+
87
+ # Save the JSON output to a file
88
+ json_file = "heart_health_report.json"
89
+ with open(json_file, "w") as f:
90
+ json.dump(output_json, f)
91
+
92
+ return result_with_disclaimer, json_file
93
+
94
+ # Create the Gradio interface
95
+ iface = gr.Interface(
96
+ fn=heart_health_bot,
97
+ inputs=[
98
+ gr.File(label="Upload a PDF, DOCX, image, or text file for analysis") # File upload input
99
+ ],
100
+ outputs=[
101
+ "text", # Output type for the bot's response
102
+ gr.File(label="Download JSON Report") # JSON download link
103
+ ],
104
+ title="HeartHealthAI Bot",
105
+ description=(
106
+ "Upload your bloodwork or Echo or ECG or any of your latest Medical Report to get an outlook for better heart health.\n\n"
107
+ "Developed by Venkat Srinivasan.\n"
108
+ "Email: venkat.srinivasan@gbs-alumni.com\n"
109
+ "LinkedIn: [Venkataraghavan Srinivasan](https://www.linkedin.com/in/venkataraghavansrinivasan/)\n\n"
110
+ "Upload a document (PDF, DOCX, image, or text) to receive a heart health report."
111
+ )
112
+ )
113
+
114
+ # Launch the Gradio interface
115
+ iface.launch()