KarthiEz commited on
Commit
80bb39b
·
verified ·
1 Parent(s): 6a80dac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -1,5 +1,3 @@
1
- is this code correct??
2
-
3
  import gradio as gr
4
  from transformers import pipeline
5
  from PIL import Image
@@ -18,40 +16,32 @@ pipe = pipeline("image-text-to-text", model="Qwen/Qwen2-VL-2B-Instruct")
18
 
19
  # ---------- robust extractor: returns ONLY the model text ----------
20
  def _only_model_text(out) -> str:
21
- # Case A: chat transcript list grab the last assistant turn
 
 
 
 
22
  if isinstance(out, list):
23
- # Prefer any assistant turn from the end
24
- for item in reversed(out):
 
 
 
 
25
  if isinstance(item, dict) and item.get("role") == "assistant":
26
  content = item.get("content")
27
  if isinstance(content, str):
28
  return content
29
  if isinstance(content, list):
30
- # collect only text segments, ignore images
31
- texts = []
32
- for seg in content:
33
- if isinstance(seg, dict) and seg.get("type") == "text":
34
- t = seg.get("text")
35
- if isinstance(t, str):
36
- texts.append(t)
37
- if texts:
38
- return "\n".join(texts)
39
- # Also handle [{'generated_text': '...'}] pattern if present
40
- for item in out:
41
- if isinstance(item, dict) and "generated_text" in item:
42
- return item["generated_text"]
43
-
44
- # Case B: dict with generated_text
45
- if isinstance(out, dict) and "generated_text" in out:
46
- return out["generated_text"]
47
-
48
- # Fallback: stringify safely
49
- try:
50
- import json as _json
51
- return _json.dumps(out, ensure_ascii=False)
52
- except Exception:
53
- return str(out)
54
-
55
 
56
  def infer(file_obj, prompt):
57
  if file_obj is None:
@@ -86,10 +76,8 @@ def infer(file_obj, prompt):
86
  out = pipe(text=messages, max_new_tokens=256)
87
 
88
  # return ONLY the assistant text
89
- out = pipe(text=messages, max_new_tokens=256)
90
  return _only_model_text(out)
91
 
92
-
93
  # ---------- Gradio UI ----------
94
  with gr.Blocks(
95
  title="Qwen2-VL-2B — File + Prompt",
@@ -105,9 +93,14 @@ with gr.Blocks(
105
  run_btn = gr.Button("Run")
106
 
107
  # output textbox that expands (via CSS above)
108
- resp_out = gr.Markdown(elem_id="resp_out")
 
 
 
 
 
109
 
110
  run_btn.click(fn=infer, inputs=[file_in, prompt_in], outputs=[resp_out])
111
 
112
  if __name__ == "__main__":
113
- demo.launch()
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  from PIL import Image
 
16
 
17
  # ---------- robust extractor: returns ONLY the model text ----------
18
  def _only_model_text(out) -> str:
19
+ # Case 1: pipelines often return {"generated_text": "..."}
20
+ if isinstance(out, dict) and "generated_text" in out:
21
+ return out["generated_text"]
22
+
23
+ # Case 2: list of dicts (mixed roles)
24
  if isinstance(out, list):
25
+ # Prefer any dict with generated_text first
26
+ for item in out:
27
+ if isinstance(item, dict) and "generated_text" in item:
28
+ return item["generated_text"]
29
+ # Otherwise find assistant role
30
+ for item in out:
31
  if isinstance(item, dict) and item.get("role") == "assistant":
32
  content = item.get("content")
33
  if isinstance(content, str):
34
  return content
35
  if isinstance(content, list):
36
+ # collect text pieces within the assistant content
37
+ chunks = []
38
+ for c in content:
39
+ if isinstance(c, dict) and c.get("type") == "text" and isinstance(c.get("text"), str):
40
+ chunks.append(c["text"])
41
+ if chunks:
42
+ return "\n".join(chunks)
43
+ # Fallback
44
+ return str(out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def infer(file_obj, prompt):
47
  if file_obj is None:
 
76
  out = pipe(text=messages, max_new_tokens=256)
77
 
78
  # return ONLY the assistant text
 
79
  return _only_model_text(out)
80
 
 
81
  # ---------- Gradio UI ----------
82
  with gr.Blocks(
83
  title="Qwen2-VL-2B — File + Prompt",
 
93
  run_btn = gr.Button("Run")
94
 
95
  # output textbox that expands (via CSS above)
96
+ resp_out = gr.Textbox(
97
+ label="Model Response",
98
+ lines=8,
99
+ show_copy_button=True,
100
+ elem_id="resp_out"
101
+ )
102
 
103
  run_btn.click(fn=infer, inputs=[file_in, prompt_in], outputs=[resp_out])
104
 
105
  if __name__ == "__main__":
106
+ demo.launch()