Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,34 +43,33 @@ GOAL:
|
|
| 43 |
Educate, inspire, and inform users about mathematics, space exploration, perseverance, and the historical context of Katherine Johnson’s contributions.
|
| 44 |
"""
|
| 45 |
|
| 46 |
-
def respond(
|
| 47 |
-
message,
|
| 48 |
-
history: list[dict[str, str]],
|
| 49 |
-
max_tokens,
|
| 50 |
-
temperature,
|
| 51 |
-
top_p,
|
| 52 |
-
):
|
| 53 |
client = InferenceClient(
|
| 54 |
-
|
| 55 |
)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
# Add
|
| 64 |
messages.append({"role": "user", "content": message})
|
| 65 |
|
| 66 |
response = ""
|
| 67 |
|
|
|
|
| 68 |
for chunk in client.chat_completion(
|
| 69 |
-
messages,
|
| 70 |
max_tokens=max_tokens,
|
| 71 |
-
stream=True,
|
| 72 |
temperature=temperature,
|
| 73 |
top_p=top_p,
|
|
|
|
| 74 |
):
|
| 75 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 76 |
token = chunk.choices[0].delta.content
|
|
@@ -82,11 +81,11 @@ chatbot = gr.ChatInterface(
|
|
| 82 |
respond,
|
| 83 |
additional_inputs=[
|
| 84 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 85 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.
|
| 86 |
gr.Slider(
|
| 87 |
minimum=0.1,
|
| 88 |
maximum=1.0,
|
| 89 |
-
value=0.
|
| 90 |
step=0.05,
|
| 91 |
label="Top-p (nucleus sampling)",
|
| 92 |
),
|
|
@@ -94,9 +93,12 @@ chatbot = gr.ChatInterface(
|
|
| 94 |
)
|
| 95 |
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
chatbot.render()
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo.launch()
|
|
|
|
|
|
| 43 |
Educate, inspire, and inform users about mathematics, space exploration, perseverance, and the historical context of Katherine Johnson’s contributions.
|
| 44 |
"""
|
| 45 |
|
| 46 |
+
def respond(message, history, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
client = InferenceClient(
|
| 48 |
+
model="meta-llama/Meta-Llama-3-8B-Instruct"
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Start conversation with system prompt
|
| 52 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 53 |
|
| 54 |
+
# Rebuild conversation history safely
|
| 55 |
+
for turn in history:
|
| 56 |
+
if turn["role"] == "user":
|
| 57 |
+
messages.append({"role": "user", "content": turn["content"]})
|
| 58 |
+
elif turn["role"] == "assistant":
|
| 59 |
+
messages.append({"role": "assistant", "content": turn["content"]})
|
| 60 |
|
| 61 |
+
# Add latest user input
|
| 62 |
messages.append({"role": "user", "content": message})
|
| 63 |
|
| 64 |
response = ""
|
| 65 |
|
| 66 |
+
# Stream response
|
| 67 |
for chunk in client.chat_completion(
|
| 68 |
+
messages=messages,
|
| 69 |
max_tokens=max_tokens,
|
|
|
|
| 70 |
temperature=temperature,
|
| 71 |
top_p=top_p,
|
| 72 |
+
stream=True,
|
| 73 |
):
|
| 74 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 75 |
token = chunk.choices[0].delta.content
|
|
|
|
| 81 |
respond,
|
| 82 |
additional_inputs=[
|
| 83 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 84 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.5, step=0.1, label="Temperature"),
|
| 85 |
gr.Slider(
|
| 86 |
minimum=0.1,
|
| 87 |
maximum=1.0,
|
| 88 |
+
value=0.9,
|
| 89 |
step=0.05,
|
| 90 |
label="Top-p (nucleus sampling)",
|
| 91 |
),
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
with gr.Blocks() as demo:
|
| 96 |
+
gr.Markdown(
|
| 97 |
+
"This is a historically grounded educational simulation of Katherine Johnson. "
|
| 98 |
+
"It is not the real person."
|
| 99 |
+
)
|
| 100 |
chatbot.render()
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|
| 104 |
+
|