Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def respond(
|
| 6 |
+
message,
|
| 7 |
+
history: list[dict[str, str]],
|
| 8 |
+
hf_token: gr.OAuthToken,
|
| 9 |
+
):
|
| 10 |
+
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 11 |
+
|
| 12 |
+
messages = [{"role": "system", "content": system_message}]
|
| 13 |
+
|
| 14 |
+
messages.extend(history)
|
| 15 |
+
|
| 16 |
+
messages.append({"role": "user", "content": message})
|
| 17 |
+
|
| 18 |
+
response = ""
|
| 19 |
+
|
| 20 |
+
for message in client.chat_completion(
|
| 21 |
+
messages,
|
| 22 |
+
max_tokens=max_tokens,
|
| 23 |
+
stream=True,
|
| 24 |
+
temperature=temperature,
|
| 25 |
+
top_p=top_p,
|
| 26 |
+
):
|
| 27 |
+
choices = message.choices
|
| 28 |
+
token = ""
|
| 29 |
+
if len(choices) and choices[0].delta.content:
|
| 30 |
+
token = choices[0].delta.content
|
| 31 |
+
|
| 32 |
+
response += token
|
| 33 |
+
yield response
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
chatbot = gr.ChatInterface(
|
| 37 |
+
respond,
|
| 38 |
+
type="messages",
|
| 39 |
+
chatbot=gr.Chatbot(collapse_thinking=[("<thinking>", "</thinking>")])
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as demo:
|
| 43 |
+
with gr.Sidebar():
|
| 44 |
+
gr.LoginButton()
|
| 45 |
+
chatbot.render()
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|