htchu Claude Haiku 5.5 commited on
Commit
98e7f1e
·
1 Parent(s): 8dd9d93

Add Gradio chat UI for coding help via OpenAI-compatible LLM

Browse files

Adds app.py (ChatInterface that streams from an OpenAI-compatible
endpoint configured through LLM_BASE_URL, LLM_API_KEY, LLM_MODEL) and
requirements.txt pinning gradio 6.31.0 to match the Space SDK version.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import gradio as gr
4
+ from openai import OpenAI
5
+
6
+ LLM_BASE_URL = os.environ.get("LLM_BASE_URL", "https://api02.webinfo.tw/v1")
7
+ LLM_API_KEY = os.environ.get("LLM_API_KEY", "")
8
+ LLM_MODEL = os.environ.get("LLM_MODEL", "Qwen/Qwen2.5-Coder-7B-Instruct-AWQ")
9
+
10
+ DEFAULT_SYSTEM_PROMPT = (
11
+ "You are a helpful programming assistant. Explain concepts clearly, "
12
+ "write correct and readable code, and point out bugs or edge cases. "
13
+ "Put code in fenced code blocks with the language name."
14
+ )
15
+
16
+ client = OpenAI(base_url=LLM_BASE_URL, api_key=LLM_API_KEY)
17
+
18
+
19
+ def respond(message, history, model, system_prompt, max_tokens, temperature):
20
+ messages = [{"role": "system", "content": system_prompt}]
21
+ messages += [{"role": m["role"], "content": m["content"]} for m in history]
22
+ messages.append({"role": "user", "content": message})
23
+
24
+ stream = client.chat.completions.create(
25
+ model=model,
26
+ messages=messages,
27
+ max_tokens=int(max_tokens),
28
+ temperature=float(temperature),
29
+ stream=True,
30
+ )
31
+
32
+ partial = ""
33
+ for chunk in stream:
34
+ if not chunk.choices:
35
+ continue
36
+ delta = chunk.choices[0].delta.content
37
+ if delta:
38
+ partial += delta
39
+ yield partial
40
+
41
+
42
+ demo = gr.ChatInterface(
43
+ fn=respond,
44
+ title="Coder Helper",
45
+ description="Ask a programming question. Answers come from a self-hosted open LLM.",
46
+ additional_inputs=[
47
+ gr.Dropdown(choices=[LLM_MODEL], value=LLM_MODEL, allow_custom_value=True, label="Model"),
48
+ gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, label="System prompt", lines=3),
49
+ gr.Slider(minimum=32, maximum=2048, value=512, step=32, label="Max new tokens"),
50
+ gr.Slider(minimum=0.0, maximum=1.5, value=0.2, step=0.1, label="Temperature"),
51
+ ],
52
+ examples=[
53
+ ["Write a Python function that checks whether a string is a palindrome."],
54
+ ["Why does a JavaScript `for (var i...)` loop with setTimeout print the same number every time?"],
55
+ ["Explain the difference between a process and a thread."],
56
+ ],
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio==6.31.0
2
+ openai