Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,64 +4,77 @@ import os
|
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
-
def respond(message, history: list[dict[str, str]]
|
| 8 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 9 |
-
|
|
|
|
| 10 |
messages.extend(history)
|
| 11 |
messages.append({"role": "user", "content": message})
|
| 12 |
|
| 13 |
response = ""
|
| 14 |
for msg in client.chat_completion(
|
| 15 |
messages,
|
| 16 |
-
max_tokens=
|
| 17 |
stream=True,
|
| 18 |
-
temperature=
|
| 19 |
-
top_p=
|
| 20 |
):
|
| 21 |
if msg.choices and msg.choices[0].delta.content:
|
| 22 |
response += msg.choices[0].delta.content
|
| 23 |
yield response
|
| 24 |
|
|
|
|
|
|
|
| 25 |
custom_css = """
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
}
|
| 42 |
-
input[type="text"] {
|
| 43 |
-
border-radius: 8px;
|
| 44 |
-
border: 1px solid #ccc;
|
| 45 |
-
}
|
| 46 |
-
button {
|
| 47 |
-
background-color: #007bff;
|
| 48 |
color: white;
|
| 49 |
-
border-radius:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
"""
|
| 52 |
|
| 53 |
-
with gr.Blocks(theme=gr.themes.Soft()
|
| 54 |
-
|
| 55 |
-
respond,
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
demo.launch(
|
|
|
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
+
def respond(message, history: list[dict[str, str]]):
|
| 8 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 9 |
+
|
| 10 |
+
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 11 |
messages.extend(history)
|
| 12 |
messages.append({"role": "user", "content": message})
|
| 13 |
|
| 14 |
response = ""
|
| 15 |
for msg in client.chat_completion(
|
| 16 |
messages,
|
| 17 |
+
max_tokens=512,
|
| 18 |
stream=True,
|
| 19 |
+
temperature=0.7,
|
| 20 |
+
top_p=0.95
|
| 21 |
):
|
| 22 |
if msg.choices and msg.choices[0].delta.content:
|
| 23 |
response += msg.choices[0].delta.content
|
| 24 |
yield response
|
| 25 |
|
| 26 |
+
|
| 27 |
+
# CSS for floating button + hidden chatbot
|
| 28 |
custom_css = """
|
| 29 |
+
#chatbot-container {
|
| 30 |
+
display: none;
|
| 31 |
+
position: fixed;
|
| 32 |
+
bottom: 80px;
|
| 33 |
+
right: 30px;
|
| 34 |
+
width: 350px;
|
| 35 |
+
max-height: 500px;
|
| 36 |
+
z-index: 9999;
|
| 37 |
+
border-radius: 15px;
|
| 38 |
+
overflow: hidden;
|
| 39 |
+
box-shadow: 0px 5px 20px rgba(0,0,0,0.3);
|
| 40 |
}
|
| 41 |
+
#chatbot-toggle {
|
| 42 |
+
position: fixed;
|
| 43 |
+
bottom: 20px;
|
| 44 |
+
right: 30px;
|
| 45 |
+
background: #7E498B;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
color: white;
|
| 47 |
+
border-radius: 50%;
|
| 48 |
+
width: 60px;
|
| 49 |
+
height: 60px;
|
| 50 |
+
font-size: 28px;
|
| 51 |
+
display: flex;
|
| 52 |
+
align-items: center;
|
| 53 |
+
justify-content: center;
|
| 54 |
+
cursor: pointer;
|
| 55 |
+
box-shadow: 0px 5px 10px rgba(0,0,0,0.2);
|
| 56 |
+
z-index: 10000;
|
| 57 |
}
|
| 58 |
"""
|
| 59 |
|
| 60 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 61 |
+
with gr.Box(elem_id="chatbot-container"):
|
| 62 |
+
gr.ChatInterface(respond, type="messages")
|
| 63 |
+
|
| 64 |
+
# Floating purple chat icon
|
| 65 |
+
gr.HTML("""
|
| 66 |
+
<div id="chatbot-toggle" onclick="toggleChat()">💬</div>
|
| 67 |
+
<script>
|
| 68 |
+
function toggleChat() {
|
| 69 |
+
var chat = document.getElementById("chatbot-container");
|
| 70 |
+
if (chat.style.display === "none" || chat.style.display === "") {
|
| 71 |
+
chat.style.display = "block";
|
| 72 |
+
} else {
|
| 73 |
+
chat.style.display = "none";
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
</script>
|
| 77 |
+
""")
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
+
demo.launch()
|