Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,77 +4,22 @@ import os
|
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
-
def respond(message, history
|
| 8 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
| 9 |
-
|
| 10 |
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 11 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 65 |
-
|
| 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()
|
|
|
|
| 4 |
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
+
def respond(message, history):
|
| 8 |
client = InferenceClient(token=HF_TOKEN, model="openai/gpt-oss-20b")
|
|
|
|
| 9 |
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 10 |
+
for h in history:
|
| 11 |
+
messages.append({"role": "user", "content": h[0]})
|
| 12 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 13 |
messages.append({"role": "user", "content": message})
|
| 14 |
|
| 15 |
response = ""
|
| 16 |
+
for msg in client.chat_completion(messages, max_tokens=200, stream=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if msg.choices and msg.choices[0].delta.content:
|
| 18 |
response += msg.choices[0].delta.content
|
| 19 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# 👇 is tarah karo taake API endpoint expose ho
|
| 22 |
+
demo = gr.ChatInterface(respond)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
| 25 |
demo.launch()
|