abidlabs HF Staff commited on
Commit
c4342b6
·
verified ·
1 Parent(s): f689fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -4
app.py CHANGED
@@ -1,11 +1,157 @@
1
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  with gr.Blocks() as demo:
4
- dropdown = gr.Dropdown(multiselect=True, choices="llama 4 (scout), gemma 3 (27b)", value="llama 4 (scout), gemma 3 (27b)")
 
 
 
 
5
  with gr.Row():
6
- image = gr.Image(type="filepath")
7
- chatbot = gr.Chatbot(type="messages")
 
 
8
  chatbot_2 = gr.Chatbot(type="messages")
9
- textbox = gr.Textbox()
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  demo.launch()
 
1
  import gradio as gr
2
+ from gradio_client.utils import encode_url_or_file_to_base64
3
+ from huggingface_hub import InferenceClient
4
+
5
+ def chat_1(history, prompt, image, oauth_token: gr.OAuthToken | None):
6
+ if oauth_token is None:
7
+ raise ValueError("Please log in to use this Space.")
8
+
9
+ if history is None:
10
+ history = []
11
+
12
+ client = InferenceClient(
13
+ provider="nebius",
14
+ api_key=oauth_token.token,
15
+ )
16
+
17
+ user_messages = [{"role": "user", "content": prompt}]
18
+ content = [
19
+ {
20
+ "type": "text",
21
+ "text": prompt
22
+ }
23
+ ]
24
+ if image is not None:
25
+ content.append({
26
+ "type": "image_url",
27
+ "image_url": {
28
+ "url": encode_url_or_file_to_base64(image)
29
+ }
30
+ })
31
+
32
+ yield history + user_messages
33
+
34
+ messages = [
35
+ {
36
+ "role": "user",
37
+ "content": content
38
+ }
39
+ ]
40
+
41
+ stream = client.chat.completions.create(
42
+ model="google/gemma-3-27b-it",
43
+ messages=messages,
44
+ max_tokens=500,
45
+ stream=True,
46
+ )
47
+
48
+ if image is not None:
49
+ user_messages.append({
50
+ "type": "image_url",
51
+ "image_url": {
52
+ "url": encode_url_or_file_to_base64(image)
53
+ }
54
+ })
55
+ response = ""
56
+ for chunk in stream:
57
+ if chunk.choices:
58
+ response += chunk.choices[0].delta.content
59
+ yield history + user_messages + [{"role": "assistant", "content": response}]
60
+
61
+
62
+ def chat_2(history, prompt, image, oauth_token: gr.OAuthToken | None):
63
+ if oauth_token is None:
64
+ raise ValueError("Please log in to use this Space.")
65
+
66
+ if history is None:
67
+ history = []
68
+
69
+ client = InferenceClient(
70
+ provider="together",
71
+ api_key=oauth_token.token,
72
+ )
73
+
74
+ user_messages = [{"role": "user", "content": prompt}]
75
+ content = [
76
+ {
77
+ "type": "text",
78
+ "text": prompt
79
+ }
80
+ ]
81
+ if image is not None:
82
+ content.append({
83
+ "type": "image_url",
84
+ "image_url": {
85
+ "url": encode_url_or_file_to_base64(image)
86
+ }
87
+ })
88
+
89
+ yield history + user_messages
90
+
91
+ messages = [
92
+ {
93
+ "role": "user",
94
+ "content": content
95
+ }
96
+ ]
97
+
98
+ stream = client.chat.completions.create(
99
+ model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
100
+ messages=messages,
101
+ max_tokens=500,
102
+ stream=True,
103
+ )
104
+
105
+ if image is not None:
106
+ user_messages.append({
107
+ "type": "image_url",
108
+ "image_url": {
109
+ "url": encode_url_or_file_to_base64(image)
110
+ }
111
+ })
112
+ response = ""
113
+ for chunk in stream:
114
+ if chunk.choices:
115
+ response += chunk.choices[0].delta.content
116
+ yield history + user_messages + [{"role": "assistant", "content": response}]
117
+
118
+
119
+ def chat_labels(models):
120
+ names = []
121
+ models = models or []
122
+ if len(models) > 0:
123
+ names.append(models[0])
124
+ else:
125
+ names.append("Chatbot 1")
126
+ if len(models) > 1:
127
+ names.append(models[1])
128
+ else:
129
+ names.append("Chatbot 2")
130
+ return gr.Chatbot(label=names[0], type="messages"), gr.Chatbot(label=names[1], type="messages")
131
 
132
  with gr.Blocks() as demo:
133
+ with gr.Sidebar():
134
+ gr.Markdown("## Smol Arena")
135
+ gr.Markdown("Welcome to Smol Arena! This is a Space that allows you to test LLMs / VLMs that have less than 30B active parameters. \n\nInference is provided by [Hugging Face Inference API](https://huggingface.co/inference-api) so please log in to use this Space.")
136
+ gr.LoginButton()
137
+ dropdown = gr.Dropdown(multiselect=True, choices=["Gemma 3 (27b)", "Llama 4 (scout)"], value=["Gemma 3 (27b)", "Llama 4 (scout)"], max_choices=2, label="Select 2 models to compare.", interactive=False)
138
  with gr.Row():
139
+ with gr.Column():
140
+ gr.Markdown("Optional image to ask the model about")
141
+ image = gr.Image(type="filepath", label="Optional Image")
142
+ chatbot_1 = gr.Chatbot(type="messages")
143
  chatbot_2 = gr.Chatbot(type="messages")
144
+ textbox = gr.Textbox(label="Prompt")
145
+
146
+
147
+ gr.on(
148
+ [dropdown.change, demo.load],
149
+ fn=chat_labels,
150
+ inputs=[dropdown],
151
+ outputs=[chatbot_1, chatbot_2],
152
+ )
153
+ textbox.submit(fn=chat_1, inputs=[chatbot_1, textbox, image], outputs=[chatbot_1])
154
+ textbox.submit(fn=chat_2, inputs=[chatbot_2, textbox, image], outputs=[chatbot_2])
155
+ textbox.submit(lambda: "", inputs=[], outputs=[textbox])
156
 
157
  demo.launch()