hponepyae commited on
Commit
b8e5a59
·
verified ·
1 Parent(s): 50aaa9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -8
app.py CHANGED
@@ -21,11 +21,11 @@ except Exception as e:
21
  model_loaded = False
22
  print(f"Error loading model: {e}")
23
 
24
- # --- Core Analysis Function ---
25
  @spaces.GPU()
26
  def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
27
  """
28
- Analyzes user's symptoms using the definitive calling convention.
29
  """
30
  if not model_loaded:
31
  return "Error: The AI model could not be loaded. Please check the Space logs."
@@ -43,7 +43,10 @@ def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
43
  )
44
 
45
  user_content = []
46
- user_content.append({"type": "text", "text": symptoms_text})
 
 
 
47
 
48
  if symptom_image:
49
  user_content.append({"type": "image", "image": symptom_image})
@@ -53,17 +56,14 @@ def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
53
  {"role": "user", "content": user_content}
54
  ]
55
 
56
- # *** THE FIX: Increased the token limit to prevent truncated output ***
57
  generation_args = {
58
- "max_new_tokens": 1024, # Increased from 512 to 1024
59
  "do_sample": True,
60
  "temperature": 0.7,
61
  }
62
 
63
- # The entire messages structure is passed to the `text` argument.
64
  output = pipe(text=messages, **generation_args)
65
 
66
- # Extract the content of the last generated message.
67
  result = output[0]["generated_text"][-1]["content"]
68
 
69
  disclaimer = "\n\n***Disclaimer: I am an AI assistant and not a medical professional. This is not a diagnosis. Please consult a doctor for any health concerns.***"
@@ -112,4 +112,4 @@ with gr.Blocks(theme=gr.themes.Soft(), title="AI Symptom Analyzer") as demo:
112
 
113
  if __name__ == "__main__":
114
  print("Starting Gradio interface...")
115
- demo.launch(debug=True)
 
21
  model_loaded = False
22
  print(f"Error loading model: {e}")
23
 
24
+ # --- Core Analysis Function (Final Robust Version) ---
25
  @spaces.GPU()
26
  def analyze_symptoms(symptom_image: Image.Image, symptoms_text: str):
27
  """
28
+ Analyzes user's symptoms with robust input handling to prevent state-related errors.
29
  """
30
  if not model_loaded:
31
  return "Error: The AI model could not be loaded. Please check the Space logs."
 
43
  )
44
 
45
  user_content = []
46
+
47
+ # *** THE FIX: Only add elements to the list if they contain content ***
48
+ if symptoms_text:
49
+ user_content.append({"type": "text", "text": symptoms_text})
50
 
51
  if symptom_image:
52
  user_content.append({"type": "image", "image": symptom_image})
 
56
  {"role": "user", "content": user_content}
57
  ]
58
 
 
59
  generation_args = {
60
+ "max_new_tokens": 1024,
61
  "do_sample": True,
62
  "temperature": 0.7,
63
  }
64
 
 
65
  output = pipe(text=messages, **generation_args)
66
 
 
67
  result = output[0]["generated_text"][-1]["content"]
68
 
69
  disclaimer = "\n\n***Disclaimer: I am an AI assistant and not a medical professional. This is not a diagnosis. Please consult a doctor for any health concerns.***"
 
112
 
113
  if __name__ == "__main__":
114
  print("Starting Gradio interface...")
115
+ demo.launch(debug=True)