yogies commited on
Commit
40940bf
Β·
verified Β·
1 Parent(s): 94678f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -61
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import gradio as gr
3
- from huggingface_hub import InferenceClient
4
 
5
  # ----------------------------------------------------------------------
6
  # Helper to read a secret (fallback is useful when you run locally)
@@ -16,66 +16,57 @@ def _secret(key: str, fallback: str = "") -> str:
16
  def respond(
17
  message: str,
18
  history: list[dict[str, str]],
19
- # max_tokens: int,
20
- # temperature: float,
21
- # top_p: float,
22
  model_name: str,
23
  ):
24
  """
25
- Generate a response using the HuggingFace Inference API.
26
-
27
  * System prompt = secret `prec_chat`
28
- * HF inference token = secret `HF_TOKEN`
29
  """
30
- # 1️⃣ Load the system prompt (fallback = generic assistant)
31
- system_message = _secret("prec_chat", "You are a helpful assistant.")
32
 
33
- # 2️⃣ Load the HF inference token
34
- hf_token = _secret("HF_TOKEN")
35
- if not hf_token:
36
  raise RuntimeError(
37
- "HF_TOKEN not found in secrets. Add it to secrets.toml (or via the Space UI)."
38
  )
39
 
40
- # 3️⃣ Initialise the HF inference client
41
- client = InferenceClient(token=hf_token, model=model_name)
 
 
 
42
 
43
- # 4️⃣ Build the message list for the chat‑completion endpoint
44
- messages = [{"role": "system", "content": system_message}]
45
  messages.extend(history) # previous conversation turns
46
  messages.append({"role": "user", "content": message}) # current user query
47
 
48
- # 5️⃣ Stream the response back to the UI
49
  response = ""
50
- for chunk in client.chat_completion(
51
- messages,
52
- # max_tokens=max_tokens,
53
- max_tokens = 8096,
54
  stream=True
55
- # temperature=temperature,
56
- # top_p=top_p,
57
- ):
58
- choices = chunk.choices
59
- token = ""
60
- if choices and choices[0].delta.content:
61
- token = choices[0].delta.content
62
- response += token
63
- yield response
64
 
65
 
66
  # ──────────────────────────────────────────────────────────────────────
67
- # 1️⃣ List of models that the UI will show.
68
- # Add new model IDs to this list whenever you want to make them
69
- # selectable – no other code changes are required.
70
  # ──────────────────────────────────────────────────────────────────────
71
  AVAILABLE_MODELS = [
72
- "deepseek-ai/DeepSeek-V3.1",
73
- # "openai/gpt-oss-20b",
74
- # "Qwen/Qwen3-4B-Thinking-2507",
75
- # "Qwen/Qwen3-30B-A3B-Thinking-2507",
76
- # "openai/gpt-oss-120b",
77
- # "Qwen/Qwen3-235B-A22B-Thinking-2507"
78
- ] # ← add more strings here (e.g. "openai/gpt-oss-350b")
79
 
80
  # ----------------------------------------------------------------------
81
  # UI – the system‑prompt textbox has been removed.
@@ -83,24 +74,14 @@ AVAILABLE_MODELS = [
83
  chatbot = gr.ChatInterface(
84
  respond,
85
  type="messages",
86
- additional_inputs=[
87
- gr.Dropdown(
88
- choices=AVAILABLE_MODELS,
89
- value=AVAILABLE_MODELS[0],
90
- label="Model",
91
- interactive=True,
92
- ),
93
- # Only generation parameters are exposed now.
94
- # gr.Slider(minimum=1, maximum=8096, value=512, step=1, label="Max new tokens"),
95
- # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
96
- # gr.Slider(
97
- # minimum=0.1,
98
- # maximum=1.0,
99
- # value=0.95,
100
- # step=0.05,
101
- # label="Top‑p (nucleus sampling)",
102
- # ),
103
- ],
104
  )
105
 
106
  # ----------------------------------------------------------------------
@@ -115,7 +96,7 @@ with gr.Blocks() as demo:
115
  # ----------------------------------------------------------------------
116
  if __name__ == "__main__":
117
  # ------------------------------------------------------------------
118
- # 1️⃣ Pull the allowed credentials from secrets (fail fast if missing)
119
  # ------------------------------------------------------------------
120
  allowed_user = _secret("CHAT_USER")
121
  allowed_pass = _secret("CHAT_PASS")
@@ -127,7 +108,7 @@ if __name__ == "__main__":
127
  )
128
 
129
  # ------------------------------------------------------------------
130
- # 2️⃣ Launch
131
  # ------------------------------------------------------------------
132
  demo.launch(
133
  auth=(allowed_user, allowed_pass), # <-- Gradio's built‑in basic auth
 
1
  import os
2
  import gradio as gr
3
+ from openai import OpenAI
4
 
5
  # ----------------------------------------------------------------------
6
  # Helper to read a secret (fallback is useful when you run locally)
 
16
  def respond(
17
  message: str,
18
  history: list[dict[str, str]],
 
 
 
19
  model_name: str,
20
  ):
21
  """
22
+ Generate a response using OpenRouter API via OpenAI client.
23
+
24
  * System prompt = secret `prec_chat`
25
+ * OpenRouter API key = secret `OPENROUTER_API_KEY`
26
  """
27
+ # 1️⃣ Load the system prompt (fallback = generic assistant)
28
+ # system_message = _secret("prec_chat", "You are a helpful assistant.")
29
 
30
+ # 2️⃣ Load the OpenRouter API key
31
+ openrouter_api_key = _secret("OPENROUTER_API_KEY")
32
+ if not openrouter_api_key:
33
  raise RuntimeError(
34
+ "OPENROUTER_API_KEY not found in secrets. Add it to secrets.toml (or via the Space UI)."
35
  )
36
 
37
+ # 3️⃣ Initialize OpenAI client with OpenRouter configuration
38
+ client = OpenAI(
39
+ base_url="https://openrouter.ai/api/v1",
40
+ api_key=openrouter_api_key,
41
+ )
42
 
43
+ # 4️⃣ Build the message list for the chat completion
44
+ messages = []
45
  messages.extend(history) # previous conversation turns
46
  messages.append({"role": "user", "content": message}) # current user query
47
 
48
+ # 5️⃣ Stream the response back to the UI
49
  response = ""
50
+ stream = client.chat.completions.create(
51
+ model=model_name,
52
+ messages=messages,
53
+ max_tokens=8096,
54
  stream=True
55
+ )
56
+
57
+ for chunk in stream:
58
+ if chunk.choices[0].delta.content is not None:
59
+ token = chunk.choices[0].delta.content
60
+ response += token
61
+ yield response
 
 
62
 
63
 
64
  # ──────────────────────────────────────────────────────────────────────
65
+ # List of models available through OpenRouter
 
 
66
  # ──────────────────────────────────────────────────────────────────────
67
  AVAILABLE_MODELS = [
68
+ "@preset/precise-chat-agent"
69
+ ]
 
 
 
 
 
70
 
71
  # ----------------------------------------------------------------------
72
  # UI – the system‑prompt textbox has been removed.
 
74
  chatbot = gr.ChatInterface(
75
  respond,
76
  type="messages",
77
+ # additional_inputs=[
78
+ # gr.Dropdown(
79
+ # choices=AVAILABLE_MODELS,
80
+ # value=AVAILABLE_MODELS[0],
81
+ # label="Model",
82
+ # interactive=True,
83
+ # ),
84
+ # ],
 
 
 
 
 
 
 
 
 
 
85
  )
86
 
87
  # ----------------------------------------------------------------------
 
96
  # ----------------------------------------------------------------------
97
  if __name__ == "__main__":
98
  # ------------------------------------------------------------------
99
+ # 1️⃣ Pull the allowed credentials from secrets (fail fast if missing)
100
  # ------------------------------------------------------------------
101
  allowed_user = _secret("CHAT_USER")
102
  allowed_pass = _secret("CHAT_PASS")
 
108
  )
109
 
110
  # ------------------------------------------------------------------
111
+ # 2️⃣ Launch
112
  # ------------------------------------------------------------------
113
  demo.launch(
114
  auth=(allowed_user, allowed_pass), # <-- Gradio's built‑in basic auth