VirusDumb commited on
Commit
3aad08f
·
1 Parent(s): 1ee6c4a

gemma 12b

Browse files
README.md CHANGED
@@ -5,7 +5,7 @@ colorFrom: green
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 6.15.2
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  license: mit
 
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 6.15.2
8
+ python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
  license: mit
__pycache__/app.cpython-313.pyc.2157280292432 ADDED
Binary file (13 kB). View file
 
app.py CHANGED
@@ -6,19 +6,31 @@ Left: a slim, collapsible chat rail. Talk to the AI, ask for an app, then ask
6
  Right: the generated app, rendered live with full JavaScript.
7
 
8
  Theme: Frutiger Aero / skeuomorphic glass.
9
- Model: Gemma 4 31B via Ollama Cloud (prototyping).
10
- Requires: pip install -U ollama agno gradio and OLLAMA_API_KEY set.
 
 
11
  """
12
 
13
  import os
14
  import re
 
 
15
  import html as html_lib
 
16
 
17
  import gradio as gr
 
18
  from agno.agent import Agent
19
- from agno.models.ollama import Ollama
20
 
21
- OLLAMA_MODEL = os.environ.get("OLLAMA_MODEL", "gemma4:31b-cloud")
 
 
 
 
 
 
22
 
23
  SYSTEM_PROMPT = """You are Discode, a friendly expert front-end engineer who builds and edits ONE single-page web app for the user through conversation.
24
 
@@ -36,10 +48,55 @@ When the user asks for a change, MODIFY the current app (it will be given to you
36
  If the user is only chatting (greeting, a question) and not asking for an app or change, reply normally with NO code block.
37
  """
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  agent = Agent(
40
- model=Ollama(id=OLLAMA_MODEL),
41
  instructions=SYSTEM_PROMPT,
42
  markdown=False,
 
43
  )
44
 
45
 
@@ -98,11 +155,6 @@ def on_send(user_msg, messages, current_html):
98
  if not user_msg or not user_msg.strip():
99
  return messages, gr.update(), current_html, ""
100
 
101
- if not os.environ.get("OLLAMA_API_KEY"):
102
- messages.append({"role": "user", "content": user_msg})
103
- messages.append({"role": "assistant", "content": "⚠️ OLLAMA_API_KEY is not set."})
104
- return messages, gr.update(), current_html, ""
105
-
106
  messages.append({"role": "user", "content": user_msg})
107
 
108
  if current_html:
@@ -113,8 +165,11 @@ def on_send(user_msg, messages, current_html):
113
  else:
114
  prompt = user_msg
115
 
 
116
  result = agent.run(prompt)
 
117
  chat_text, new_html = parse_response(result.content)
 
118
 
119
  messages.append({"role": "assistant", "content": chat_text})
120
 
@@ -203,7 +258,6 @@ with gr.Blocks(css=AERO_CSS, theme=gr.themes.Soft(), title="Discode") as demo:
203
  # Left: slim chat rail
204
  with gr.Column(scale=2, min_width=280, elem_id="chat-col") as chat_col:
205
  chatbot = gr.Chatbot(
206
- type="messages",
207
  height="62vh",
208
  elem_id="chatbox",
209
  show_label=False,
 
6
  Right: the generated app, rendered live with full JavaScript.
7
 
8
  Theme: Frutiger Aero / skeuomorphic glass.
9
+ Model: Gemma 4 12B via llama.cpp.
10
+ Local: start `llama-server -hf ggml-org/gemma-4-12B-it-GGUF:Q4_K_M --jinja -c 4096`
11
+ before running this app, or install llama-cpp-python so the app can spawn it.
12
+ Space: the app spawns llama_cpp.server on CPU Basic unless a server is already running.
13
  """
14
 
15
  import os
16
  import re
17
+ import sys
18
+ import time
19
  import html as html_lib
20
+ import subprocess
21
 
22
  import gradio as gr
23
+ import requests
24
  from agno.agent import Agent
25
+ from agno.models.llama_cpp import LlamaCpp
26
 
27
+ MODEL_REPO = os.environ.get("MODEL_REPO", "ggml-org/gemma-4-12B-it-GGUF")
28
+ MODEL_FILE = os.environ.get("MODEL_FILE", "gemma-4-12B-it-Q4_K_M.gguf")
29
+ HOST = os.environ.get("LLAMACPP_HOST", "127.0.0.1")
30
+ PORT = int(os.environ.get("LLAMACPP_PORT", "8080"))
31
+ BASE_URL = os.environ.get("LLAMACPP_BASE_URL", f"http://{HOST}:{PORT}/v1")
32
+ N_CTX = os.environ.get("LLAMACPP_CTX", "4096")
33
+ N_THREADS = os.environ.get("LLAMACPP_THREADS", "2")
34
 
35
  SYSTEM_PROMPT = """You are Discode, a friendly expert front-end engineer who builds and edits ONE single-page web app for the user through conversation.
36
 
 
48
  If the user is only chatting (greeting, a question) and not asking for an app or change, reply normally with NO code block.
49
  """
50
 
51
+
52
+ def server_is_up() -> bool:
53
+ try:
54
+ return requests.get(f"{BASE_URL}/models", timeout=2).status_code == 200
55
+ except requests.exceptions.RequestException:
56
+ return False
57
+
58
+
59
+ server_process = None
60
+ if server_is_up():
61
+ print(f"[startup] Found llama.cpp server at {BASE_URL}", flush=True)
62
+ else:
63
+ print("[startup] No llama.cpp server found; downloading GGUF and spawning llama_cpp.server ...", flush=True)
64
+ from huggingface_hub import hf_hub_download
65
+
66
+ model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
67
+ server_process = subprocess.Popen(
68
+ [
69
+ sys.executable,
70
+ "-m",
71
+ "llama_cpp.server",
72
+ "--model",
73
+ model_path,
74
+ "--host",
75
+ HOST,
76
+ "--port",
77
+ str(PORT),
78
+ "--n_ctx",
79
+ N_CTX,
80
+ "--n_threads",
81
+ N_THREADS,
82
+ ]
83
+ )
84
+
85
+ print("[startup] Waiting for llama.cpp server ...", flush=True)
86
+ for _ in range(360):
87
+ if server_is_up():
88
+ print("[startup] llama.cpp server is ready.", flush=True)
89
+ break
90
+ time.sleep(1)
91
+ else:
92
+ raise RuntimeError("llama.cpp server did not start in time")
93
+
94
+
95
  agent = Agent(
96
+ model=LlamaCpp(id=MODEL_FILE, base_url=BASE_URL, api_key="sk-no-key-needed"),
97
  instructions=SYSTEM_PROMPT,
98
  markdown=False,
99
+ debug_mode=True,
100
  )
101
 
102
 
 
155
  if not user_msg or not user_msg.strip():
156
  return messages, gr.update(), current_html, ""
157
 
 
 
 
 
 
158
  messages.append({"role": "user", "content": user_msg})
159
 
160
  if current_html:
 
165
  else:
166
  prompt = user_msg
167
 
168
+ started = time.time()
169
  result = agent.run(prompt)
170
+ elapsed = time.time() - started
171
  chat_text, new_html = parse_response(result.content)
172
+ chat_text = f"{chat_text}\n\n_responded in {elapsed:.1f}s via Gemma 4 12B Q4 on llama.cpp_"
173
 
174
  messages.append({"role": "assistant", "content": chat_text})
175
 
 
258
  # Left: slim chat rail
259
  with gr.Column(scale=2, min_width=280, elem_id="chat-col") as chat_col:
260
  chatbot = gr.Chatbot(
 
261
  height="62vh",
262
  elem_id="chatbox",
263
  show_label=False,
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  gradio
2
  agno
3
- ollama
4
- openai
 
 
1
  gradio
2
  agno
3
+ huggingface_hub
4
+ llama-cpp-python[server]
5
+ requests