R1000 commited on
Commit
e78286a
·
verified ·
1 Parent(s): 7ae2e3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -105,7 +105,6 @@ def process_image(image, task_name):
105
  return "❌ โมเดลยังไม่ได้โหลดหรือเกิดข้อผิดพลาดในการเริ่มต้น"
106
  if image is None:
107
  return "กรุณาเลือกรูปภาพ"
108
-
109
  try:
110
  # แปลง Gradio Image เป็น PIL Image
111
  if not isinstance(image, Image.Image):
@@ -115,7 +114,6 @@ def process_image(image, task_name):
115
 
116
  # ดึง Prompt จาก Task ที่เลือก
117
  prompt = TASK_PROMPTS.get(task_name, "<CAPTION>")
118
-
119
  inputs = florence_processor(text=prompt, images=image, return_tensors="pt").to(device)
120
 
121
  generated_ids = florence_model.generate(
@@ -126,26 +124,32 @@ def process_image(image, task_name):
126
  do_sample=False,
127
  num_beams=3,
128
  )
129
-
130
  generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
131
  parsed_answer = florence_processor.post_process_generation(
132
  generated_text, task=prompt, image_size=(image.width, image.height)
133
  )
 
 
 
 
 
 
 
 
 
 
134
 
135
- # คืนค่าผลลัพธ์ (อาจเป็น dict หรือ str แล้วแต่ task)
136
- result = str(parsed_answer)
137
- print(f"\n✅ Task: {task_name} | Prompt: {prompt}")
138
- return result
139
 
140
  except Exception as e:
141
  return f"❌ เกิดข้อผิดพลาดขณะประมวลผล: {str(e)}"
142
 
143
  # --- สร้าง UI ---
144
- with gr.Blocks(title="Image-to-Prompt (Florence-2 NSFW)", theme=gr.themes.Soft()) as demo:
145
  gr.Markdown("""
146
- # 🖼️ Image-to-Prompt (Florence-2 NSFW)
147
  อัปโหลดรูปภาพและเลือกประเภทการวิเคราะห์เพื่อสร้าง Prompt
148
- *ใช้โมเดล NSFW: `ljnlonoljpiljm/florence-2-base-nsfw-v2`*
149
  """)
150
 
151
  with gr.Row():
 
105
  return "❌ โมเดลยังไม่ได้โหลดหรือเกิดข้อผิดพลาดในการเริ่มต้น"
106
  if image is None:
107
  return "กรุณาเลือกรูปภาพ"
 
108
  try:
109
  # แปลง Gradio Image เป็น PIL Image
110
  if not isinstance(image, Image.Image):
 
114
 
115
  # ดึง Prompt จาก Task ที่เลือก
116
  prompt = TASK_PROMPTS.get(task_name, "<CAPTION>")
 
117
  inputs = florence_processor(text=prompt, images=image, return_tensors="pt").to(device)
118
 
119
  generated_ids = florence_model.generate(
 
124
  do_sample=False,
125
  num_beams=3,
126
  )
 
127
  generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
128
  parsed_answer = florence_processor.post_process_generation(
129
  generated_text, task=prompt, image_size=(image.width, image.height)
130
  )
131
+
132
+ # --- ส่วนแก้ไข: แยกค่าออกจาก Dictionary ---
133
+ # parsed_answer จะเป็น dict เช่น {'<DETAILED_CAPTION>': 'ข้อความ...'}
134
+ # เราต้องการแค่ 'ข้อความ...'
135
+ if isinstance(parsed_answer, dict):
136
+ # ดึงค่าแรก (Value) จาก Dictionary
137
+ result_text = next(iter(parsed_answer.values()))
138
+ else:
139
+ # ถ้าไม่ใช่ dict (กรณีบาง task อาจ return string โดยตรง)
140
+ result_text = str(parsed_answer)
141
 
142
+ print(f"\n✅ Task: {task_name} | Result: {result_text}")
143
+ return result_text
 
 
144
 
145
  except Exception as e:
146
  return f"❌ เกิดข้อผิดพลาดขณะประมวลผล: {str(e)}"
147
 
148
  # --- สร้าง UI ---
149
+ with gr.Blocks(title="Image-to-Prompt (Florence-2)", theme=gr.themes.Soft()) as demo:
150
  gr.Markdown("""
151
+ # 🖼️ Image-to-Prompt (Florence-2)
152
  อัปโหลดรูปภาพและเลือกประเภทการวิเคราะห์เพื่อสร้าง Prompt
 
153
  """)
154
 
155
  with gr.Row():