Vimby commited on
Commit
d1dc52c
·
verified ·
1 Parent(s): 52b62a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -53,41 +53,40 @@ Educate, inspire, and inform users about mathematics, space exploration, perseve
53
  """
54
 
55
  def respond(message, history, max_tokens, temperature, top_p):
56
- client = InferenceClient(
57
- model="openai/gpt-oss-20b",
58
- token=os.environ["HF_TOKEN"],
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- # Start conversation with system prompt
62
- messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
-
64
- # Rebuild conversation history safely
65
- for turn in history:
66
- if turn["role"] == "user":
67
- messages.append({"role": "user", "content": turn["content"]})
68
- elif turn["role"] == "assistant":
69
- messages.append({"role": "assistant", "content": turn["content"]})
70
-
71
- # Add latest user input
72
- messages.append({"role": "user", "content": message})
73
-
74
- response = ""
75
-
76
- # Stream response
77
- for chunk in client.chat_completion(
78
- messages=messages,
79
- max_tokens=max_tokens,
80
- temperature=temperature,
81
- top_p=top_p,
82
- stream=True,
83
- ):
84
- if chunk.choices and chunk.choices[0].delta.content:
85
- token = chunk.choices[0].delta.content
86
- response += token
87
- yield response
88
-
89
  except Exception as e:
90
- yield f"ERROR: {str(e)}"
 
91
 
92
  chatbot = gr.ChatInterface(
93
  respond,
 
53
  """
54
 
55
  def respond(message, history, max_tokens, temperature, top_p):
56
+ try:
57
+ client = InferenceClient(
58
+ model="openai/gpt-oss-20b",
59
+ token=os.environ["HF_TOKEN"],
60
+ )
61
+
62
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
63
+
64
+ # Rebuild conversation history safely
65
+ for turn in history:
66
+ if turn["role"] == "user":
67
+ messages.append({"role": "user", "content": turn["content"]})
68
+ elif turn["role"] == "assistant":
69
+ messages.append({"role": "assistant", "content": turn["content"]})
70
+
71
+ messages.append({"role": "user", "content": message})
72
+
73
+ response = ""
74
+
75
+ for chunk in client.chat_completion(
76
+ messages=messages,
77
+ max_tokens=max_tokens,
78
+ temperature=temperature,
79
+ top_p=top_p,
80
+ stream=True,
81
+ ):
82
+ if chunk.choices and chunk.choices[0].delta.content:
83
+ token = chunk.choices[0].delta.content
84
+ response += token
85
+ yield response
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  except Exception as e:
88
+ yield f"ERROR: {str(e)}"
89
+
90
 
91
  chatbot = gr.ChatInterface(
92
  respond,