bethmanik commited on
Commit
ce8db20
·
verified ·
1 Parent(s): d6da02e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -18,7 +18,7 @@ You do not claim to literally be Katherine Johnson. You are a historically groun
18
  SPEAKING STYLE AND TONE:
19
  - Calm, precise, and composed
20
  - Intellectually confident but humble
21
- - Encouraging toward students
22
  - Professional and reflective of mid-20th-century academic speech
23
  - No modern slang, memes, emojis, or internet-style phrasing
24
 
@@ -36,7 +36,7 @@ BOUNDARIES:
36
  - Do not fabricate events, dialogue, or achievements.
37
  - If a question falls outside documented knowledge, acknowledge uncertainty.
38
  - Avoid speculation about thoughts or private conversations unless historically supported.
39
- - Stay in character and do not shift into modern AI commentary unless relevant to character’s history
40
  - Do not provide unsafe technical guidance.
41
 
42
  GOAL:
@@ -46,48 +46,43 @@ Educate, inspire, and inform users about mathematics, space exploration, perseve
46
  def respond(
47
  message,
48
  history: list[dict[str, str]],
49
- system_message,
50
  max_tokens,
51
  temperature,
52
  top_p,
53
  hf_token: gr.OAuthToken,
54
  ):
55
- """
56
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
57
- """
58
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
59
 
60
- messages = [{"role": "system", "content": system_message}]
 
61
 
 
62
  messages.extend(history)
63
 
 
64
  messages.append({"role": "user", "content": message})
65
 
66
  response = ""
67
 
68
- for message in client.chat_completion(
69
  messages,
70
  max_tokens=max_tokens,
71
  stream=True,
72
  temperature=temperature,
73
  top_p=top_p,
74
  ):
75
- choices = message.choices
76
- token = ""
77
- if len(choices) and choices[0].delta.content:
78
- token = choices[0].delta.content
79
 
80
- response += token
81
- yield response
82
 
83
-
84
- """
85
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
86
- """
87
  chatbot = gr.ChatInterface(
88
  respond,
89
  additional_inputs=[
90
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
91
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
92
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
93
  gr.Slider(
@@ -105,6 +100,5 @@ with gr.Blocks() as demo:
105
  gr.LoginButton()
106
  chatbot.render()
107
 
108
-
109
  if __name__ == "__main__":
110
  demo.launch()
 
18
  SPEAKING STYLE AND TONE:
19
  - Calm, precise, and composed
20
  - Intellectually confident but humble
21
+ - Encouraging toward students
22
  - Professional and reflective of mid-20th-century academic speech
23
  - No modern slang, memes, emojis, or internet-style phrasing
24
 
 
36
  - Do not fabricate events, dialogue, or achievements.
37
  - If a question falls outside documented knowledge, acknowledge uncertainty.
38
  - Avoid speculation about thoughts or private conversations unless historically supported.
39
+ - Stay in character and do not shift into modern AI commentary unless relevant to character’s history.
40
  - Do not provide unsafe technical guidance.
41
 
42
  GOAL:
 
46
  def respond(
47
  message,
48
  history: list[dict[str, str]],
 
49
  max_tokens,
50
  temperature,
51
  top_p,
52
  hf_token: gr.OAuthToken,
53
  ):
54
+ client = InferenceClient(
55
+ token=hf_token.token,
56
+ model="openai/gpt-oss-20b",
57
+ )
58
 
59
+ # Always start with system prompt
60
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
61
 
62
+ # Add conversation history
63
  messages.extend(history)
64
 
65
+ # Add current user message
66
  messages.append({"role": "user", "content": message})
67
 
68
  response = ""
69
 
70
+ for chunk in client.chat_completion(
71
  messages,
72
  max_tokens=max_tokens,
73
  stream=True,
74
  temperature=temperature,
75
  top_p=top_p,
76
  ):
77
+ if chunk.choices and chunk.choices[0].delta.content:
78
+ token = chunk.choices[0].delta.content
79
+ response += token
80
+ yield response
81
 
 
 
82
 
 
 
 
 
83
  chatbot = gr.ChatInterface(
84
  respond,
85
  additional_inputs=[
 
86
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
87
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
88
  gr.Slider(
 
100
  gr.LoginButton()
101
  chatbot.render()
102
 
 
103
  if __name__ == "__main__":
104
  demo.launch()