Spaces:
Sleeping
Sleeping
Commit
·
9780e52
0
Parent(s):
Importing files from HF Spaces
Browse files- README.md +15 -0
- app.py +131 -0
- requirements.txt +2 -0
README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: CSDS553 Demo
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
hf_oauth: true
|
| 11 |
+
hf_oauth_scopes:
|
| 12 |
+
- inference-api
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
|
app.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
pipe = None
|
| 6 |
+
stop_inference = False
|
| 7 |
+
|
| 8 |
+
# Fancy styling
|
| 9 |
+
fancy_css = """
|
| 10 |
+
#main-container {
|
| 11 |
+
background-color: #f0f0f0;
|
| 12 |
+
font-family: 'Arial', sans-serif;
|
| 13 |
+
}
|
| 14 |
+
.gradio-container {
|
| 15 |
+
max-width: 700px;
|
| 16 |
+
margin: 0 auto;
|
| 17 |
+
padding: 20px;
|
| 18 |
+
background: white;
|
| 19 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 20 |
+
border-radius: 10px;
|
| 21 |
+
}
|
| 22 |
+
.gr-button {
|
| 23 |
+
background-color: #4CAF50;
|
| 24 |
+
color: white;
|
| 25 |
+
border: none;
|
| 26 |
+
border-radius: 5px;
|
| 27 |
+
padding: 10px 20px;
|
| 28 |
+
cursor: pointer;
|
| 29 |
+
transition: background-color 0.3s ease;
|
| 30 |
+
}
|
| 31 |
+
.gr-button:hover {
|
| 32 |
+
background-color: #45a049;
|
| 33 |
+
}
|
| 34 |
+
.gr-slider input {
|
| 35 |
+
color: #4CAF50;
|
| 36 |
+
}
|
| 37 |
+
.gr-chat {
|
| 38 |
+
font-size: 16px;
|
| 39 |
+
}
|
| 40 |
+
#title {
|
| 41 |
+
text-align: center;
|
| 42 |
+
font-size: 2em;
|
| 43 |
+
margin-bottom: 20px;
|
| 44 |
+
color: #333;
|
| 45 |
+
}
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
def respond(
|
| 49 |
+
message,
|
| 50 |
+
history: list[dict[str, str]],
|
| 51 |
+
system_message,
|
| 52 |
+
max_tokens,
|
| 53 |
+
temperature,
|
| 54 |
+
top_p,
|
| 55 |
+
hf_token: gr.OAuthToken,
|
| 56 |
+
use_local_model: bool,
|
| 57 |
+
):
|
| 58 |
+
global pipe
|
| 59 |
+
|
| 60 |
+
# Build messages from history
|
| 61 |
+
messages = [{"role": "system", "content": system_message}]
|
| 62 |
+
messages.extend(history)
|
| 63 |
+
messages.append({"role": "user", "content": message})
|
| 64 |
+
|
| 65 |
+
response = ""
|
| 66 |
+
|
| 67 |
+
if use_local_model:
|
| 68 |
+
print("[MODE] local")
|
| 69 |
+
from transformers import pipeline
|
| 70 |
+
import torch
|
| 71 |
+
if pipe is None:
|
| 72 |
+
pipe = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct")
|
| 73 |
+
|
| 74 |
+
# Build prompt as plain text
|
| 75 |
+
prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages])
|
| 76 |
+
|
| 77 |
+
outputs = pipe(
|
| 78 |
+
prompt,
|
| 79 |
+
max_new_tokens=max_tokens,
|
| 80 |
+
do_sample=True,
|
| 81 |
+
temperature=temperature,
|
| 82 |
+
top_p=top_p,
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
response = outputs[0]["generated_text"][len(prompt):]
|
| 86 |
+
yield response.strip()
|
| 87 |
+
|
| 88 |
+
else:
|
| 89 |
+
print("[MODE] api")
|
| 90 |
+
|
| 91 |
+
if hf_token is None or not getattr(hf_token, "token", None):
|
| 92 |
+
yield "⚠️ Please log in with your Hugging Face account first."
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 96 |
+
|
| 97 |
+
for chunk in client.chat_completion(
|
| 98 |
+
messages,
|
| 99 |
+
max_tokens=max_tokens,
|
| 100 |
+
stream=True,
|
| 101 |
+
temperature=temperature,
|
| 102 |
+
top_p=top_p,
|
| 103 |
+
):
|
| 104 |
+
choices = chunk.choices
|
| 105 |
+
token = ""
|
| 106 |
+
if len(choices) and choices[0].delta.content:
|
| 107 |
+
token = choices[0].delta.content
|
| 108 |
+
response += token
|
| 109 |
+
yield response
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
chatbot = gr.ChatInterface(
|
| 113 |
+
fn=respond,
|
| 114 |
+
additional_inputs=[
|
| 115 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 116 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 117 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 118 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 119 |
+
gr.Checkbox(label="Use Local Model", value=False),
|
| 120 |
+
],
|
| 121 |
+
type="messages",
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
with gr.Blocks(css=fancy_css) as demo:
|
| 125 |
+
with gr.Row():
|
| 126 |
+
gr.Markdown("<h1 style='text-align: center;'>🌟 Fancy AI Chatbot 🌟</h1>")
|
| 127 |
+
gr.LoginButton()
|
| 128 |
+
chatbot.render()
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|