Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
"""
|
| 15 |
-
|
|
|
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# GPT Chatbot powered by HuggingFace Inference API
|
| 3 |
+
# Clean, safe, production-ready for Hugging Face Spaces
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import time
|
| 7 |
import gradio as gr
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
|
| 10 |
+
# -----------------------
|
| 11 |
+
# Configuration
|
| 12 |
+
# -----------------------
|
| 13 |
+
HF_TOKEN = os.getenv("HF_API_TOKEN")
|
| 14 |
+
GPT_MODEL_ID = os.getenv("GPT_MODEL_ID", "HuggingFaceH4/zephyr-7b-beta")
|
| 15 |
+
|
| 16 |
+
if HF_TOKEN is None:
|
| 17 |
+
raise ValueError("❌ Please define HF_API_TOKEN in Hugging Face Secrets.")
|
| 18 |
+
|
| 19 |
+
# Create inference client
|
| 20 |
+
client = InferenceClient(
|
| 21 |
+
model=GPT_MODEL_ID,
|
| 22 |
+
token=HF_TOKEN,
|
| 23 |
+
)
|
| 24 |
|
| 25 |
+
# -----------------------
|
| 26 |
+
# Chatbot Core
|
| 27 |
+
# -----------------------
|
| 28 |
+
def gpt_chat(prompt: str, history, thinking_mode=False, thinking_time=2.0):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
+
Generate a chat response using GPT model on HuggingFace Inference API.
|
| 31 |
+
Clean, without chain-of-thought exposure.
|
| 32 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
if not prompt.strip():
|
| 35 |
+
return history, "لطفاً پیام بنویسید."
|
| 36 |
+
|
| 37 |
+
# Optional "thinking" simulation
|
| 38 |
+
if thinking_mode:
|
| 39 |
+
time.sleep(thinking_time)
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Build clean conversation prompt
|
| 43 |
+
conversation_text = ""
|
| 44 |
+
for user_msg, bot_msg in history:
|
| 45 |
+
conversation_text += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 46 |
+
conversation_text += f"User: {prompt}\nAssistant:"
|
| 47 |
+
|
| 48 |
+
# Query HuggingFace API
|
| 49 |
+
response = client.text_generation(
|
| 50 |
+
prompt=conversation_text,
|
| 51 |
+
max_new_tokens=200,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Clean output
|
| 57 |
+
answer = response.replace(conversation_text, "").strip()
|
| 58 |
+
answer = answer.split("Assistant:")[-1].strip()
|
| 59 |
+
|
| 60 |
+
# Update history
|
| 61 |
+
history.append((prompt, answer))
|
| 62 |
+
return history, "OK"
|
| 63 |
+
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print("Error:", e)
|
| 66 |
+
history.append((prompt, "⚠ خطا در ارتباط با مدل."))
|
| 67 |
+
return history, str(e)
|
| 68 |
+
|
| 69 |
+
# -----------------------
|
| 70 |
+
# Gradio UI
|
| 71 |
+
# -----------------------
|
| 72 |
+
with gr.Blocks(title="GPT Chatbot") as demo:
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
# 🤖 GPT Chatbot (HuggingFace Inference API)
|
| 75 |
+
مدل: **{GPT_MODEL_ID}**
|
| 76 |
+
|
| 77 |
+
این چتبات از API رسمی هاگینگفیس استفاده میکند.
|
| 78 |
+
""")
|
| 79 |
+
|
| 80 |
+
chatbot = gr.Chatbot(label="Chat")
|
| 81 |
+
user_input = gr.Textbox(placeholder="پیام خود را بنویسید...", show_label=False)
|
| 82 |
+
thinking_mode = gr.Checkbox(label="حالت فکر کردن", value=True)
|
| 83 |
+
thinking_time = gr.Slider(0, 5, value=2, step=0.5, label="مدت فکر کردن (ثانیه)")
|
| 84 |
+
status_box = gr.Textbox(label="وضعیت")
|
| 85 |
+
|
| 86 |
+
def respond(history, msg, thinking, thinking_t):
|
| 87 |
+
return gpt_chat(msg, history, thinking, thinking_t)
|
| 88 |
|
| 89 |
+
user_input.submit(
|
| 90 |
+
respond,
|
| 91 |
+
inputs=[chatbot, user_input, thinking_mode, thinking_time],
|
| 92 |
+
outputs=[chatbot, status_box]
|
| 93 |
+
)
|
| 94 |
|
| 95 |
+
demo.launch()
|
|
|