Novaciano commited on
Commit
9938e73
·
verified ·
1 Parent(s): 672f3c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -0
app.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+ # SPDX-FileCopyrightText: Hadad <[email protected]>
3
+ # SPDX-License-Identifier: Apache-2.0
4
+ #
5
+
6
+ import os
7
+ from ollama import AsyncClient
8
+ import gradio as gr
9
+
10
+ async def playground(
11
+ message,
12
+ history,
13
+ num_ctx,
14
+ temperature,
15
+ repeat_penalty,
16
+ min_p,
17
+ top_k,
18
+ top_p
19
+ ):
20
+ if not isinstance(message, str) or not message.strip():
21
+ yield []
22
+ return
23
+
24
+ client = AsyncClient(
25
+ host=os.getenv("OLLAMA_API_BASE_URL"),
26
+ headers={
27
+ "Authorization": f"Bearer {os.getenv('OLLAMA_API_KEY')}"
28
+ }
29
+ )
30
+
31
+ messages = []
32
+ for item in history:
33
+ if isinstance(item, dict) and "role" in item and "content" in item:
34
+ messages.append({
35
+ "role": item["role"],
36
+ "content": item["content"]
37
+ })
38
+ messages.append({"role": "user", "content": message})
39
+
40
+ response = ""
41
+ async for part in await client.chat(
42
+ model="gemma3:270m",
43
+ messages=messages,
44
+ options={
45
+ "num_ctx": int(num_ctx),
46
+ "temperature": float(temperature),
47
+ "repeat_penalty": float(repeat_penalty),
48
+ "min_p": float(min_p),
49
+ "top_k": int(top_k),
50
+ "top_p": float(top_p)
51
+ },
52
+ stream=True
53
+ ):
54
+ response += part.get("message", {}).get("content", "")
55
+ yield response
56
+
57
+ with gr.Blocks(
58
+ fill_height=True,
59
+ fill_width=True
60
+ ) as app:
61
+ with gr.Sidebar():
62
+ gr.Markdown("## Ollama Playground by UltimaX Intelligence")
63
+ gr.HTML(
64
+ """
65
+ This space run the <b><a href=
66
+ "https://huggingface.co/google/gemma-3-270m"
67
+ target="_blank">Gemma 3 (270M)</a></b> model from
68
+ <b>Google</b>, hosted on a server using <b>Ollama</b> and
69
+ accessed via the <b>Ollama Python SDK</b>.<br><br>
70
+
71
+ Official <b>documentation</b> for using Ollama with the
72
+ Python SDK can be found
73
+ <b><a href="https://github.com/ollama/ollama-python"
74
+ target="_blank">here</a></b>.<br><br>
75
+
76
+ Gemma 3 (270M) runs entirely on <b>CPU</b>, utilizing only a
77
+ <b>single core</b>. Thanks to its small size, the model can
78
+ operate efficiently on minimal hardware.<br><br>
79
+
80
+ The Gemma 3 (270M) model can also be viewed or downloaded
81
+ from the official Ollama website
82
+ <b><a href="https://ollama.com/library/gemma3:270m"
83
+ target="_blank">here</a></b>.<br><br>
84
+
85
+ While Gemma 3 has multimodal capabilities, running it on CPU
86
+ with a relatively small number of parameters may limit its
87
+ contextual understanding. For this reason, the upload
88
+ functionality has been disabled.<br><br>
89
+
90
+ <b>Like this project? You can support me by buying a
91
+ <a href="https://ko-fi.com/hadad" target="_blank">
92
+ coffee</a></b>.
93
+ """
94
+ )
95
+ gr.Markdown("---")
96
+ gr.Markdown("## Model Parameters")
97
+ num_ctx = gr.Slider(
98
+ minimum=512,
99
+ maximum=1024,
100
+ value=512,
101
+ step=128,
102
+ label="Context Length (num_ctx)",
103
+ info="Maximum context window size. Limited to CPU usage."
104
+ )
105
+ gr.Markdown("")
106
+ temperature = gr.Slider(
107
+ minimum=0.1,
108
+ maximum=2.0,
109
+ value=1.0,
110
+ step=0.1,
111
+ label="Temperature",
112
+ info="Controls randomness in generation"
113
+ )
114
+ gr.Markdown("")
115
+ repeat_penalty = gr.Slider(
116
+ minimum=0.1,
117
+ maximum=2.0,
118
+ value=1.0,
119
+ step=0.1,
120
+ label="Repeat Penalty",
121
+ info="Penalty for repeating tokens"
122
+ )
123
+ gr.Markdown("")
124
+ min_p = gr.Slider(
125
+ minimum=0.0,
126
+ maximum=1.0,
127
+ value=0.001,
128
+ step=0.001,
129
+ label="Min P",
130
+ info="Minimum probability threshold"
131
+ )
132
+ gr.Markdown("")
133
+ top_k = gr.Slider(
134
+ minimum=0,
135
+ maximum=100,
136
+ value=64,
137
+ step=1,
138
+ label="Top K",
139
+ info="Number of top tokens to consider"
140
+ )
141
+ gr.Markdown("")
142
+ top_p = gr.Slider(
143
+ minimum=0.0,
144
+ maximum=1.0,
145
+ value=0.95,
146
+ step=0.05,
147
+ label="Top P",
148
+ info="Cumulative probability threshold"
149
+ )
150
+ gr.ChatInterface(
151
+ fn=playground,
152
+ additional_inputs=[
153
+ num_ctx,
154
+ temperature,
155
+ repeat_penalty,
156
+ min_p,
157
+ top_k,
158
+ top_p
159
+ ],
160
+ chatbot=gr.Chatbot(
161
+ label="Ollama | Gemma 3 (270M)",
162
+ type="messages",
163
+ show_copy_button=True,
164
+ scale=1
165
+ ),
166
+ type="messages",
167
+ examples=[
168
+ ["Please introduce yourself."],
169
+ ["What caused World War II?"],
170
+ ["Give me a short introduction to large language model."],
171
+ ["Explain about quantum computers."]
172
+ ],
173
+ cache_examples=False,
174
+ show_api=False
175
+ )
176
+
177
+ app.launch(
178
+ server_name="0.0.0.0",
179
+ pwa=True
180
+ )