Nihal2000 commited on
Commit
dfdc0c0
·
1 Parent(s): 96d5236

fixed response bug

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -4,14 +4,7 @@ from huggingface_hub import InferenceClient
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
 
7
- def respond(
8
- message,
9
- history: list[dict[str, str]],
10
- system_message,
11
- max_tokens,
12
- temperature,
13
- top_p,
14
- ):
15
  client = InferenceClient(model="Nihal2000/gemma3-merged", token=HF_TOKEN)
16
 
17
  messages = [{"role": "system", "content": system_message}]
@@ -19,19 +12,25 @@ def respond(
19
  messages.append({"role": "user", "content": message})
20
 
21
  response = ""
22
- for message in client.chat_completion(
23
- messages,
24
- max_tokens=max_tokens,
25
- stream=True,
26
- temperature=temperature,
27
- top_p=top_p,
28
- ):
29
- choices = message.choices
30
- token = ""
31
- if len(choices) and choices[0].delta.content:
32
- token = choices[0].delta.content
33
- response += token
34
- yield response
 
 
 
 
 
 
35
 
36
 
37
  chatbot = gr.ChatInterface(
 
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
 
7
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
 
 
 
 
 
 
 
8
  client = InferenceClient(model="Nihal2000/gemma3-merged", token=HF_TOKEN)
9
 
10
  messages = [{"role": "system", "content": system_message}]
 
12
  messages.append({"role": "user", "content": message})
13
 
14
  response = ""
15
+ try:
16
+ for msg in client.chat_completion(
17
+ messages,
18
+ max_tokens=max_tokens,
19
+ stream=True,
20
+ temperature=temperature,
21
+ top_p=top_p,
22
+ ):
23
+ choices = msg.choices
24
+ token = ""
25
+ if len(choices) and choices[0].delta.content:
26
+ token = choices[0].delta.content
27
+ response += token
28
+ yield response
29
+ except Exception as e:
30
+ yield f"[Error] {str(e)}"
31
+
32
+ if not response:
33
+ yield "[No response from model]"
34
 
35
 
36
  chatbot = gr.ChatInterface(