AIencoder commited on
Commit
9cb16d2
·
verified ·
1 Parent(s): c08d3bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -59
app.py CHANGED
@@ -6,22 +6,37 @@ from faster_whisper import WhisperModel
6
  OLLAMA_URL = "http://localhost:11434"
7
 
8
  MODELS = {
9
- "Qwen2.5-Coder 1.5B (Fastest)": "qwen2.5-coder:1.5b",
10
- "Qwen2.5-Coder 3B (Fast)": "qwen2.5-coder:3b",
11
- "Qwen2.5-Coder 7B (Quality)": "qwen2.5-coder:7b",
12
- "Qwen3-Coder 30B-A3B (Best)": "hf.co/bartowski/Qwen_Qwen3-30B-A3B-GGUF:Q4_K_M",
 
 
 
 
 
 
13
  }
14
 
 
 
 
 
 
 
15
  print("Loading Whisper...")
16
  whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
17
  print("Whisper ready!")
18
 
19
- def check_ollama():
20
  try:
21
  r = requests.get(f"{OLLAMA_URL}/api/tags", timeout=2)
22
- return r.status_code == 200
 
 
23
  except:
24
- return False
 
25
 
26
  def transcribe_audio(audio):
27
  if audio is None:
@@ -33,8 +48,12 @@ def transcribe_audio(audio):
33
  return f"[STT Error: {e}]"
34
 
35
  def chat_stream(message, history, model_name, temperature, max_tokens):
 
 
 
 
36
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
37
- messages = [{"role": "system", "content": "You are an expert coding assistant. Always use markdown code blocks."}]
38
 
39
  for user_msg, assistant_msg in history:
40
  messages.append({"role": "user", "content": user_msg})
@@ -57,21 +76,31 @@ def chat_stream(message, history, model_name, temperature, max_tokens):
57
  data = json.loads(line)
58
  if "message" in data:
59
  full += data["message"].get("content", "")
60
- yield full
61
  except:
62
  continue
63
  except Exception as e:
64
- yield f"Error: {e}"
65
 
66
- def generate_code(prompt, language, model_name, max_tokens):
67
- if not prompt.strip(): return "Please describe what you want."
 
 
68
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
69
- full_prompt = f"Write {language} code for: {prompt}\n\nOutput ONLY code in a markdown block."
 
 
 
 
 
 
 
 
70
 
71
  try:
72
  r = requests.post(
73
  f"{OLLAMA_URL}/api/generate",
74
- json={"model": model, "prompt": full_prompt, "stream": False, "options": {"temperature": 0.3, "num_predict": max_tokens}},
75
  timeout=300
76
  )
77
  if r.status_code == 200:
@@ -79,92 +108,174 @@ def generate_code(prompt, language, model_name, max_tokens):
79
  if "```" in result:
80
  parts = result.split("```")
81
  if len(parts) >= 2:
82
- return parts[1].split("\n", 1)[-1].strip() if "\n" in parts[1] else parts[1]
 
 
 
83
  return result
84
- return f"Error: {r.text}"
85
  except Exception as e:
86
- return f"Error: {e}"
87
 
88
- def explain_code(code, model_name, max_tokens):
89
- if not code.strip(): return "Paste code to explain."
 
 
90
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
 
 
 
 
 
 
 
 
 
 
91
  try:
92
  r = requests.post(
93
  f"{OLLAMA_URL}/api/generate",
94
- json={"model": model, "prompt": f"Explain this code:\n```\n{code}\n```", "stream": False, "options": {"num_predict": max_tokens}},
95
  timeout=300
96
  )
97
- return r.json().get("response", "") if r.status_code == 200 else f"Error: {r.text}"
98
  except Exception as e:
99
- return f"Error: {e}"
100
 
101
  def fix_code(code, error, model_name, max_tokens):
102
- if not code.strip(): return "Paste code to fix."
 
 
103
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
104
- prompt = f"Fix this code:\n```\n{code}\n```\nError: {error or 'Not working'}"
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  try:
106
  r = requests.post(
107
  f"{OLLAMA_URL}/api/generate",
108
  json={"model": model, "prompt": prompt, "stream": False, "options": {"temperature": 0.3, "num_predict": max_tokens}},
109
  timeout=300
110
  )
111
- return r.json().get("response", "") if r.status_code == 200 else f"Error: {r.text}"
112
  except Exception as e:
113
- return f"Error: {e}"
114
 
115
- with gr.Blocks(title="Axon v5.1", theme=gr.themes.Soft(primary_hue="purple")) as demo:
116
- gr.Markdown("# 🔥 Axon v5.1\n**Ollama Edition** • Qwen2.5-Coder + Qwen3-Coder")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  with gr.Row():
119
- model_dropdown = gr.Dropdown(choices=list(MODELS.keys()), value="Qwen2.5-Coder 3B (Fast)", label="🤖 Model")
120
- temperature = gr.Slider(0, 1, value=0.7, step=0.1, label="Temp")
121
- max_tokens = gr.Slider(256, 4096, value=2048, step=256, label="Max Tokens")
122
 
123
  with gr.Tabs():
 
124
  with gr.TabItem("💬 Chat"):
125
- chatbot = gr.Chatbot(height=400)
126
  with gr.Row():
127
- msg = gr.Textbox(placeholder="Ask about coding...", show_label=False, scale=7)
128
- audio_input = gr.Audio(sources=["microphone"], type="filepath", label="🎤", scale=2)
129
  send = gr.Button("Send", variant="primary", scale=1)
130
  with gr.Row():
131
- clear = gr.Button("Clear")
132
- transcribe_btn = gr.Button("🎤 Transcribe", variant="secondary")
133
- gr.Examples(["Write a Python quicksort function"], inputs=msg)
134
-
 
 
135
  with gr.TabItem("⚡ Generate"):
136
  with gr.Row():
137
- with gr.Column():
138
- gen_prompt = gr.Textbox(label="Describe request", lines=3)
139
- gen_lang = gr.Dropdown(["Python", "JavaScript", "Go", "Rust", "C++"], value="Python", label="Language")
140
- gen_btn = gr.Button("Generate", variant="primary")
141
- gen_output = gr.Code(label="Code", language="python", lines=15)
142
-
 
 
143
  with gr.TabItem("🔍 Explain"):
144
  with gr.Row():
145
- explain_input = gr.Code(label="Paste code", lines=10)
146
- explain_output = gr.Markdown()
147
- explain_btn = gr.Button("Explain", variant="primary")
148
-
 
 
 
149
  with gr.TabItem("🔧 Fix"):
150
  with gr.Row():
151
- with gr.Column():
152
- fix_input = gr.Code(label="Buggy code", lines=10)
153
- fix_error = gr.Textbox(label="Error message", lines=2)
154
- fix_btn = gr.Button("Fix", variant="primary")
155
- fix_output = gr.Markdown()
 
 
 
 
 
 
 
 
 
156
 
157
  def respond(message, history, model, temp, tokens):
158
  history = history or []
159
- for chunk in chat_stream(message, history, model, temp, tokens):
160
- yield history + [[message, chunk]], ""
161
-
162
  msg.submit(respond, [msg, chatbot, model_dropdown, temperature, max_tokens], [chatbot, msg])
163
  send.click(respond, [msg, chatbot, model_dropdown, temperature, max_tokens], [chatbot, msg])
164
  clear.click(lambda: [], None, chatbot)
165
  transcribe_btn.click(transcribe_audio, audio_input, msg)
166
- gen_btn.click(generate_code, [gen_prompt, gen_lang, model_dropdown, max_tokens], gen_output)
167
- explain_btn.click(explain_code, [explain_input, model_dropdown, max_tokens], explain_output)
168
  fix_btn.click(fix_code, [fix_input, fix_error, model_dropdown, max_tokens], fix_output)
 
169
 
170
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
6
  OLLAMA_URL = "http://localhost:11434"
7
 
8
  MODELS = {
9
+ "⭐ Qwen3 30B-A3B (Best)": "hf.co/bartowski/Qwen_Qwen3-30B-A3B-GGUF:Q4_K_M",
10
+ "Qwen2.5 Coder 7B": "qwen2.5-coder:7b",
11
+ "Qwen2.5 Coder 3B": "qwen2.5-coder:3b",
12
+ "Qwen2.5 Coder 1.5B (Fast)": "qwen2.5-coder:1.5b",
13
+ "DeepSeek Coder 6.7B": "deepseek-coder:6.7b",
14
+ "DeepSeek Coder 1.3B (Fast)": "deepseek-coder:1.3b",
15
+ "StarCoder2 7B": "starcoder2:7b",
16
+ "StarCoder2 3B": "starcoder2:3b",
17
+ "CodeGemma 7B": "codegemma:7b",
18
+ "CodeGemma 2B (Fast)": "codegemma:2b",
19
  }
20
 
21
+ LANGUAGES = [
22
+ "Python", "JavaScript", "TypeScript", "Go", "Rust",
23
+ "Java", "C++", "C#", "PHP", "Ruby", "Swift", "Kotlin",
24
+ "HTML/CSS", "SQL", "Bash", "PowerShell"
25
+ ]
26
+
27
  print("Loading Whisper...")
28
  whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
29
  print("Whisper ready!")
30
 
31
+ def get_status():
32
  try:
33
  r = requests.get(f"{OLLAMA_URL}/api/tags", timeout=2)
34
+ if r.status_code == 200:
35
+ models = r.json().get("models", [])
36
+ return f"✅ Online | {len(models)} models loaded"
37
  except:
38
+ pass
39
+ return "⏳ Starting..."
40
 
41
  def transcribe_audio(audio):
42
  if audio is None:
 
48
  return f"[STT Error: {e}]"
49
 
50
  def chat_stream(message, history, model_name, temperature, max_tokens):
51
+ if not message.strip():
52
+ yield history
53
+ return
54
+
55
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
56
+ messages = [{"role": "system", "content": "You are an expert coding assistant. Provide clear, well-commented code. Always use markdown code blocks with language tags."}]
57
 
58
  for user_msg, assistant_msg in history:
59
  messages.append({"role": "user", "content": user_msg})
 
76
  data = json.loads(line)
77
  if "message" in data:
78
  full += data["message"].get("content", "")
79
+ yield history + [[message, full]]
80
  except:
81
  continue
82
  except Exception as e:
83
+ yield history + [[message, f"Error: {e}"]]
84
 
85
+ def generate_code(prompt, language, model_name, temperature, max_tokens):
86
+ if not prompt.strip():
87
+ return "⚠️ Please describe what you want to build."
88
+
89
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
90
+ full_prompt = f"""Write {language} code for the following task:
91
+
92
+ {prompt}
93
+
94
+ Requirements:
95
+ - Clean, production-ready code
96
+ - Add helpful comments
97
+ - Handle edge cases
98
+ - Output ONLY the code in a markdown code block"""
99
 
100
  try:
101
  r = requests.post(
102
  f"{OLLAMA_URL}/api/generate",
103
+ json={"model": model, "prompt": full_prompt, "stream": False, "options": {"temperature": temperature, "num_predict": max_tokens}},
104
  timeout=300
105
  )
106
  if r.status_code == 200:
 
108
  if "```" in result:
109
  parts = result.split("```")
110
  if len(parts) >= 2:
111
+ code = parts[1]
112
+ if "\n" in code:
113
+ code = code.split("\n", 1)[-1]
114
+ return code.strip()
115
  return result
116
+ return f"Error: {r.text}"
117
  except Exception as e:
118
+ return f"Error: {e}"
119
 
120
+ def explain_code(code, model_name, detail_level, max_tokens):
121
+ if not code.strip():
122
+ return "⚠️ Paste code to explain."
123
+
124
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
125
+
126
+ detail_prompts = {
127
+ "Brief": "Give a brief 2-3 sentence explanation of what this code does.",
128
+ "Normal": "Explain what this code does, including the main logic and any important details.",
129
+ "Detailed": "Give a detailed explanation including: purpose, how it works step-by-step, time/space complexity, and potential improvements."
130
+ }
131
+
132
+ prompt = f"""{detail_prompts[detail_level]}
133
+ {code}
134
+
135
  try:
136
  r = requests.post(
137
  f"{OLLAMA_URL}/api/generate",
138
+ json={"model": model, "prompt": prompt, "stream": False, "options": {"num_predict": max_tokens}},
139
  timeout=300
140
  )
141
+ return r.json().get("response", "") if r.status_code == 200 else f"Error: {r.text}"
142
  except Exception as e:
143
+ return f"Error: {e}"
144
 
145
  def fix_code(code, error, model_name, max_tokens):
146
+ if not code.strip():
147
+ return "⚠️ Paste code to fix."
148
+
149
  model = MODELS.get(model_name, "qwen2.5-coder:3b")
150
+ prompt = f"""Fix the following code and explain what was wrong.
151
+
152
+ Code:
153
+ ```
154
+ {code}
155
+ ```
156
+
157
+ Error/Problem: {error or 'Code is not working as expected'}
158
+
159
+ Provide:
160
+ 1. The fixed code
161
+ 2. Brief explanation of what was wrong
162
+ 3. Any suggestions to prevent similar issues"""
163
+
164
  try:
165
  r = requests.post(
166
  f"{OLLAMA_URL}/api/generate",
167
  json={"model": model, "prompt": prompt, "stream": False, "options": {"temperature": 0.3, "num_predict": max_tokens}},
168
  timeout=300
169
  )
170
+ return r.json().get("response", "") if r.status_code == 200 else f"Error: {r.text}"
171
  except Exception as e:
172
+ return f"Error: {e}"
173
 
174
+ def review_code(code, model_name, max_tokens):
175
+ if not code.strip():
176
+ return "⚠️ Paste code to review."
177
+
178
+ model = MODELS.get(model_name, "qwen2.5-coder:3b")
179
+ prompt = f"""Review this code and provide feedback on:
180
+
181
+ 1. **Code Quality** - Is it clean, readable, well-structured?
182
+ 2. **Bugs/Issues** - Any potential bugs or problems?
183
+ 3. **Performance** - Any performance concerns?
184
+ 4. **Security** - Any security issues?
185
+ 5. **Suggestions** - How could it be improved?
186
+ {code}
187
+
188
+ try:
189
+ r = requests.post(
190
+ f"{OLLAMA_URL}/api/generate",
191
+ json={"model": model, "prompt": prompt, "stream": False, "options": {"temperature": 0.4, "num_predict": max_tokens}},
192
+ timeout=300
193
+ )
194
+ return r.json().get("response", "") if r.status_code == 200 else f"❌ Error: {r.text}"
195
+ except Exception as e:
196
+ return f"❌ Error: {e}"
197
+
198
+ css = """
199
+ .container { max-width: 1200px; margin: auto; }
200
+ footer { display: none !important; }
201
+ """
202
+
203
+ with gr.Blocks(title="Axon v6", css=css, theme=gr.themes.Soft(primary_hue="violet", secondary_hue="blue")) as demo:
204
+
205
+ gr.Markdown("""
206
+ # 🔥 Axon v6
207
+ ### AI Coding Assistant • 10 Models • 100% Local • No Rate Limits
208
+ """)
209
+
210
+ status = gr.Markdown(value=get_status, every=5)
211
 
212
  with gr.Row():
213
+ model_dropdown = gr.Dropdown(choices=list(MODELS.keys()), value="Qwen2.5 Coder 3B", label="🤖 Model", scale=3)
214
+ temperature = gr.Slider(0, 1, value=0.7, step=0.1, label="🌡️ Creativity", scale=2)
215
+ max_tokens = gr.Slider(256, 8192, value=2048, step=256, label="📏 Max Length", scale=2)
216
 
217
  with gr.Tabs():
218
+
219
  with gr.TabItem("💬 Chat"):
220
+ chatbot = gr.Chatbot(height=450, show_copy_button=True)
221
  with gr.Row():
222
+ msg = gr.Textbox(placeholder="Ask a coding question...", show_label=False, scale=8, lines=2)
 
223
  send = gr.Button("Send", variant="primary", scale=1)
224
  with gr.Row():
225
+ audio_input = gr.Audio(sources=["microphone"], type="filepath", label="🎤 Voice")
226
+ transcribe_btn = gr.Button("�� Transcribe")
227
+ clear = gr.Button("🗑️ Clear")
228
+ with gr.Accordion("💡 Examples", open=False):
229
+ gr.Examples(["Write a Python quicksort function", "Explain async/await in JavaScript"], inputs=msg)
230
+
231
  with gr.TabItem("⚡ Generate"):
232
  with gr.Row():
233
+ with gr.Column(scale=1):
234
+ gen_prompt = gr.Textbox(label="📝 Describe what you want", lines=4)
235
+ gen_lang = gr.Dropdown(LANGUAGES, value="Python", label="🔤 Language")
236
+ gen_temp = gr.Slider(0, 1, value=0.3, step=0.1, label="🌡️ Creativity")
237
+ gen_btn = gr.Button("⚡ Generate", variant="primary", size="lg")
238
+ with gr.Column(scale=2):
239
+ gen_output = gr.Code(label="Generated Code", language="python", lines=20)
240
+
241
  with gr.TabItem("🔍 Explain"):
242
  with gr.Row():
243
+ with gr.Column(scale=1):
244
+ explain_input = gr.Code(label="📋 Paste code", lines=12)
245
+ explain_detail = gr.Radio(["Brief", "Normal", "Detailed"], value="Normal", label="📊 Detail")
246
+ explain_btn = gr.Button("🔍 Explain", variant="primary", size="lg")
247
+ with gr.Column(scale=1):
248
+ explain_output = gr.Markdown(label="Explanation")
249
+
250
  with gr.TabItem("🔧 Fix"):
251
  with gr.Row():
252
+ with gr.Column(scale=1):
253
+ fix_input = gr.Code(label="🐛 Buggy code", lines=10)
254
+ fix_error = gr.Textbox(label="Error message", lines=3)
255
+ fix_btn = gr.Button("🔧 Fix", variant="primary", size="lg")
256
+ with gr.Column(scale=1):
257
+ fix_output = gr.Markdown(label="Fixed Code")
258
+
259
+ with gr.TabItem("📋 Review"):
260
+ with gr.Row():
261
+ with gr.Column(scale=1):
262
+ review_input = gr.Code(label="📋 Code to review", lines=15)
263
+ review_btn = gr.Button("📋 Review", variant="primary", size="lg")
264
+ with gr.Column(scale=1):
265
+ review_output = gr.Markdown(label="Review")
266
 
267
  def respond(message, history, model, temp, tokens):
268
  history = history or []
269
+ for updated_history in chat_stream(message, history, model, temp, tokens):
270
+ yield updated_history, ""
271
+
272
  msg.submit(respond, [msg, chatbot, model_dropdown, temperature, max_tokens], [chatbot, msg])
273
  send.click(respond, [msg, chatbot, model_dropdown, temperature, max_tokens], [chatbot, msg])
274
  clear.click(lambda: [], None, chatbot)
275
  transcribe_btn.click(transcribe_audio, audio_input, msg)
276
+ gen_btn.click(generate_code, [gen_prompt, gen_lang, model_dropdown, gen_temp, max_tokens], gen_output)
277
+ explain_btn.click(explain_code, [explain_input, model_dropdown, explain_detail, max_tokens], explain_output)
278
  fix_btn.click(fix_code, [fix_input, fix_error, model_dropdown, max_tokens], fix_output)
279
+ review_btn.click(review_code, [review_input, model_dropdown, max_tokens], review_output)
280
 
281
+ demo.launch(server_name="0.0.0.0", server_port=7860)