Hkb2001 commited on
Commit
7753c8d
·
1 Parent(s): aa4861f

remove thinking

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -2,21 +2,19 @@ import gradio as gr
2
  import fitz
3
  import os
4
  import easyocr
 
5
  from huggingface_hub import InferenceClient
6
 
7
  HF_API_KEY = os.getenv("HF_API_KEY")
8
 
9
- # Initialize the Hugging Face Inference Client
10
  client = InferenceClient(
11
- provider="together", # Specify provider
12
  api_key=HF_API_KEY
13
  )
14
 
15
- # Initialize EasyOCR
16
  reader = easyocr.Reader(['en'])
17
 
18
  def extract_text_from_pdf(pdf_file):
19
- """Extracts text from the uploaded PDF file."""
20
  text = ""
21
  with fitz.open(pdf_file.name) as doc:
22
  for page in doc:
@@ -24,12 +22,10 @@ def extract_text_from_pdf(pdf_file):
24
  return text.strip()
25
 
26
  def extract_text_from_image(image_file):
27
- """Extracts text from an uploaded image file using EasyOCR."""
28
  text = reader.readtext(image_file.name, detail=0)
29
  return "\n".join(text).strip()
30
 
31
  def analyze_medical_report(file):
32
- """Determines file type (PDF or Image) and extracts text accordingly."""
33
  if file.name.lower().endswith(".pdf"):
34
  text = extract_text_from_pdf(file)
35
  else:
@@ -38,35 +34,41 @@ def analyze_medical_report(file):
38
  if not text:
39
  return "No text found in the uploaded document."
40
 
41
- # Construct message for DeepSeek AI
42
  messages = [
43
  {"role": "user", "content": f"""
44
- Analyze the following medical report and provide a structured response:
45
-
46
- 1. **Short Description**: Briefly summarize the report in 2-3 sentences.
47
- 2. **Key Concerns**: List any health issues found.
48
- 3. **Recommendations**: Provide necessary medical advice or next steps.
49
-
50
- Report:
51
- {text}
52
- """}
 
 
 
 
 
 
53
  ]
54
 
55
  try:
56
- # Send request to Hugging Face Inference API
57
  completion = client.chat.completions.create(
58
  model="deepseek-ai/DeepSeek-R1",
59
  messages=messages,
60
  max_tokens=500,
61
  )
62
 
63
- # Extract response
64
- return completion.choices[0].message.content if completion.choices else "No response generated."
 
 
 
65
 
66
  except Exception as e:
67
  return f"Error: {str(e)}"
68
 
69
- # Gradio Interface
70
  interface = gr.Interface(
71
  fn=analyze_medical_report,
72
  inputs=gr.File(type="filepath", label="Upload Medical Report (PDF/Image)"),
 
2
  import fitz
3
  import os
4
  import easyocr
5
+ import re
6
  from huggingface_hub import InferenceClient
7
 
8
  HF_API_KEY = os.getenv("HF_API_KEY")
9
 
 
10
  client = InferenceClient(
11
+ provider="together",
12
  api_key=HF_API_KEY
13
  )
14
 
 
15
  reader = easyocr.Reader(['en'])
16
 
17
  def extract_text_from_pdf(pdf_file):
 
18
  text = ""
19
  with fitz.open(pdf_file.name) as doc:
20
  for page in doc:
 
22
  return text.strip()
23
 
24
  def extract_text_from_image(image_file):
 
25
  text = reader.readtext(image_file.name, detail=0)
26
  return "\n".join(text).strip()
27
 
28
  def analyze_medical_report(file):
 
29
  if file.name.lower().endswith(".pdf"):
30
  text = extract_text_from_pdf(file)
31
  else:
 
34
  if not text:
35
  return "No text found in the uploaded document."
36
 
 
37
  messages = [
38
  {"role": "user", "content": f"""
39
+ Analyze the following medical report and provide a structured response in the exact format below.
40
+ Ensure the response follows this structure, dont use extra space
41
+ **Short Description:**
42
+ [Briefly summarize the report in 2-3 sentences. Include key test details.]
43
+ **Key Concerns:**
44
+ explain in Ordered form. First tell the test name value and tell is it high or low. Keep it short
45
+ 2-3 main concerns are enough
46
+ 1. **[Test Name (Value)]:** [High or low Explain what the abnormality suggests.]
47
+
48
+ **Recommendations:**
49
+ Give in bullet points. Give only 2-3 and dont write too much explanation
50
+
51
+ Medical Report:
52
+ {text}
53
+ """}
54
  ]
55
 
56
  try:
 
57
  completion = client.chat.completions.create(
58
  model="deepseek-ai/DeepSeek-R1",
59
  messages=messages,
60
  max_tokens=500,
61
  )
62
 
63
+ output = completion.choices[0].message.content if completion.choices else "No response generated."
64
+
65
+ output = re.sub(r"<think>.*?</think>", "", output, flags=re.DOTALL).strip()
66
+
67
+ return output
68
 
69
  except Exception as e:
70
  return f"Error: {str(e)}"
71
 
 
72
  interface = gr.Interface(
73
  fn=analyze_medical_report,
74
  inputs=gr.File(type="filepath", label="Upload Medical Report (PDF/Image)"),