Hkb2001 commited on
Commit
7ba23aa
·
1 Parent(s): 938eefc

initial commit

Browse files
Files changed (2) hide show
  1. app.py +69 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz
3
+ import easyocr
4
+ from huggingface_hub import InferenceClient
5
+
6
+ HUGGINGFACE_API_KEY = "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"
7
+
8
+ # Initialize the Hugging Face Inference Client
9
+ client = InferenceClient(
10
+ provider="together", # Specify provider
11
+ api_key=HUGGINGFACE_API_KEY
12
+ )
13
+
14
+ # Initialize EasyOCR
15
+ reader = easyocr.Reader(['en'])
16
+
17
+ def extract_text_from_pdf(pdf_file):
18
+ """Extracts text from the uploaded PDF file."""
19
+ text = ""
20
+ with fitz.open(pdf_file.name) as doc:
21
+ for page in doc:
22
+ text += page.get_text() + "\n"
23
+ return text.strip()
24
+
25
+ def extract_text_from_image(image_file):
26
+ """Extracts text from an uploaded image file using EasyOCR."""
27
+ text = reader.readtext(image_file.name, detail=0)
28
+ return "\n".join(text).strip()
29
+
30
+ def analyze_medical_report(file):
31
+ """Determines file type (PDF or Image) and extracts text accordingly."""
32
+ if file.name.lower().endswith(".pdf"):
33
+ text = extract_text_from_pdf(file)
34
+ else:
35
+ text = extract_text_from_image(file)
36
+
37
+ if not text:
38
+ return "No text found in the uploaded document."
39
+
40
+ # Construct message for DeepSeek AI
41
+ messages = [
42
+ {"role": "user", "content": f"Analyze the following medical report and identify any health concerns:\n\n{text}"}
43
+ ]
44
+
45
+ try:
46
+ # Send request to Hugging Face Inference API
47
+ completion = client.chat.completions.create(
48
+ model="deepseek-ai/DeepSeek-R1",
49
+ messages=messages,
50
+ max_tokens=500,
51
+ )
52
+
53
+ # Extract response
54
+ return completion.choices[0].message.content if completion.choices else "No response generated."
55
+
56
+ except Exception as e:
57
+ return f"Error: {str(e)}"
58
+
59
+ # Gradio Interface
60
+ interface = gr.Interface(
61
+ fn=analyze_medical_report,
62
+ inputs=gr.File(type="filepath", label="Upload Medical Report (PDF/Image)"),
63
+ outputs=gr.Textbox(label="AI Analysis"),
64
+ title="AI-Powered Medical Report Analyzer",
65
+ description="Upload your medical report (PDF or Image), and the AI will analyze it to identify potential issues."
66
+ )
67
+
68
+ if __name__ == "__main__":
69
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ requests
3
+ pymupdf
4
+ easyocr
5
+ torch
6
+ transformers