willian166 commited on
Commit
2c01af9
·
verified ·
1 Parent(s): a97bda0

Complete rebuild: single file, no external deps, gr.ChatInterface

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, spaces
2
+ spaces.GPU(lambda: None)()
3
+
4
+ import gradio as gr
5
+ from huggingface_hub import InferenceClient
6
+
7
+ MODEL = os.environ.get("SKILLS_MODEL", "meta-llama/Llama-3.1-8B-Instruct")
8
+ TOKEN = os.environ.get("OPENAI_API_KEY", "")
9
+
10
+ SYS = "You are SkillBot, helping users create Codex skills. Keep answers warm, ask 1-2 questions. Speak Chinese if user speaks Chinese."
11
+
12
+ def chat(message, history):
13
+ msgs = [{"role": "system", "content": SYS}]
14
+ for h in (history or []):
15
+ if isinstance(h, dict):
16
+ msgs.append(h)
17
+ elif isinstance(h, (list, tuple)):
18
+ msgs.append({"role": "user", "content": h[0]})
19
+ if h[1]: msgs.append({"role": "assistant", "content": h[1]})
20
+ msgs.append({"role": "user", "content": message})
21
+
22
+ client = InferenceClient(model=MODEL, token=TOKEN)
23
+ resp = client.chat_completion(messages=msgs, max_tokens=1024, temperature=0.7)
24
+ reply = resp.choices[0].message.content
25
+ history.append({"role": "user", "content": message})
26
+ history.append({"role": "assistant", "content": reply})
27
+ return history
28
+
29
+ demo = gr.ChatInterface(
30
+ fn=chat,
31
+ chatbot=gr.Chatbot(type="messages", height=500),
32
+ title="SkillBot",
33
+ description="Create Codex skills by chatting. Describe your idea in plain language.",
34
+ )
35
+ demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))