ericjedha commited on
Commit
1ff4c6a
·
verified ·
1 Parent(s): fc38ce3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -101
app.py CHANGED
@@ -1,106 +1,12 @@
1
  import gradio as gr
2
- import torch
3
- from threading import Thread
4
- from transformers import (
5
- AutoProcessor,
6
- AutoModelForImageTextToText,
7
- TextIteratorStreamer,
8
- )
9
-
10
- device = "cuda" if torch.cuda.is_available() else "cpu"
11
 
12
- processor = AutoProcessor.from_pretrained(
13
- "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
14
- )
15
 
16
- model = AutoModelForImageTextToText.from_pretrained(
17
- "HuggingFaceTB/SmolVLM2-2.2B-Instruct",
18
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
 
19
  )
20
- model.to(device)
21
-
22
-
23
- def infer(text, files, history, max_tokens):
24
- user_content = []
25
-
26
- if text.strip():
27
- user_content.append({"type": "text", "text": text})
28
-
29
- if files:
30
- for f in files:
31
- name = f.name
32
- if name.lower().endswith((".png", ".jpg", ".jpeg")):
33
- user_content.append({"type": "image", "path": name})
34
- elif name.lower().endswith(".mp4"):
35
- user_content.append({"type": "video", "path": name})
36
-
37
- messages = [{"role": "user", "content": user_content}]
38
-
39
- inputs = processor.apply_chat_template(
40
- messages,
41
- add_generation_prompt=True,
42
- tokenize=True,
43
- return_tensors="pt",
44
- ).to(device)
45
-
46
- streamer = TextIteratorStreamer(
47
- processor, skip_prompt=True, skip_special_tokens=True
48
- )
49
-
50
- thread = Thread(
51
- target=model.generate,
52
- kwargs=dict(
53
- inputs,
54
- streamer=streamer,
55
- max_new_tokens=max_tokens,
56
- ),
57
- )
58
- thread.start()
59
-
60
- output = ""
61
- for token in streamer:
62
- output += token
63
- yield output
64
-
65
-
66
- with gr.Blocks() as demo:
67
- gr.Markdown("## 🧠 SmolVLM2 – Stable Space UI")
68
-
69
- chatbot = gr.Chatbot(height=450)
70
-
71
- text = gr.Textbox(
72
- label="Your question",
73
- placeholder="Ask something about the image or video",
74
- )
75
-
76
- files = gr.Files(
77
- label="Upload image or video",
78
- file_types=["image", ".mp4"],
79
- )
80
-
81
- max_tokens = gr.Slider(50, 400, value=200, step=50)
82
-
83
- def user_submit(t, f, history):
84
- history = history + [[t, None]]
85
- return "", [], history
86
-
87
- def bot_reply(history, max_tokens):
88
- last_text = history[-1][0]
89
- for chunk in infer(last_text, files.value, history[:-1], max_tokens):
90
- history[-1][1] = chunk
91
- yield history
92
-
93
- submit = gr.Button("Send")
94
-
95
- submit.click(
96
- user_submit,
97
- [text, files, chatbot],
98
- [text, files, chatbot],
99
- queue=False,
100
- ).then(
101
- bot_reply,
102
- [chatbot, max_tokens],
103
- chatbot,
104
- )
105
 
106
- demo.launch(debug=True)
 
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
2
 
3
+ def hello(x):
4
+ return f"Hello {x}"
 
5
 
6
+ demo = gr.Interface(
7
+ fn=hello,
8
+ inputs=gr.Textbox(),
9
+ outputs=gr.Textbox(),
10
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ demo.launch()