Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,42 @@
|
|
| 1 |
-
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
|
| 6 |
-
# Hugging Face model + token
|
| 7 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
-
MODEL = "openai/gpt-oss-20b"
|
| 9 |
-
client = InferenceClient(token=HF_TOKEN, model=MODEL)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
messages
|
| 17 |
-
for user, bot in history:
|
| 18 |
-
messages.append({"role": "user", "content": user})
|
| 19 |
-
messages.append({"role": "assistant", "content": bot})
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
response = ""
|
| 23 |
-
for msg in client.chat_completion(
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# Handle input
|
| 47 |
-
def user_submit(user_message, history):
|
| 48 |
-
history = history + [[user_message, None]]
|
| 49 |
-
return "", history
|
| 50 |
-
|
| 51 |
-
def bot_response(history, temperature, top_p, max_tokens):
|
| 52 |
-
user_message = history[-1][0]
|
| 53 |
-
response = respond(user_message, history[:-1], temperature, top_p, max_tokens)
|
| 54 |
-
for partial in response:
|
| 55 |
-
history[-1][1] = partial
|
| 56 |
-
yield history
|
| 57 |
-
|
| 58 |
-
msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
|
| 59 |
-
bot_response, [chatbot, temperature, top_p, max_tokens], chatbot
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
clear.click(lambda: [], None, chatbot, queue=False)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def respond(message, history: list[dict[str, str]]):
|
| 7 |
+
# Fixed hidden system message
|
| 8 |
+
system_message = "You are a friendly Chatbot named Persillia AI. You love coding and talking about airplanes."
|
| 9 |
+
|
| 10 |
+
client = InferenceClient(model="openai/gpt-oss-20b")
|
| 11 |
|
| 12 |
+
# Build messages with hidden system
|
| 13 |
+
messages = [{"role": "system", "content": system_message}]
|
| 14 |
+
messages.extend(history)
|
|
|
|
|
|
|
|
|
|
| 15 |
messages.append({"role": "user", "content": message})
|
| 16 |
|
| 17 |
response = ""
|
| 18 |
+
for msg in client.chat_completion(
|
| 19 |
+
messages,
|
| 20 |
+
max_tokens=512, # hidden setting
|
| 21 |
+
stream=True,
|
| 22 |
+
temperature=0.7, # hidden setting
|
| 23 |
+
top_p=0.95, # hidden setting
|
| 24 |
+
):
|
| 25 |
+
choices = msg.choices
|
| 26 |
+
if choices and choices[0].delta.content:
|
| 27 |
+
response += choices[0].delta.content
|
| 28 |
+
yield response
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
chatbot = gr.ChatInterface(
|
| 32 |
+
respond,
|
| 33 |
+
type="messages",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
with gr.Blocks(css=".gradio-container {background-color: #0d1117; color: white;}") as demo:
|
| 37 |
+
gr.Markdown("<h1 style='text-align:center;'>✈️ Persillia AI</h1>")
|
| 38 |
+
with gr.Row():
|
| 39 |
+
chatbot.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
demo.launch()
|