Spaces:
Sleeping
Sleeping
File size: 3,507 Bytes
9d5a5aa a9d3da2 9d5a5aa 6bd8b92 9d5a5aa a9d3da2 6bd8b92 9d5a5aa a9d3da2 6bd8b92 a9d3da2 6bd8b92 a9d3da2 6bd8b92 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa a9d3da2 9d5a5aa c4f521a 9d5a5aa c4f521a a9d3da2 9d5a5aa a9d3da2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# app.py
# GPT Chatbot with thinking mode
# Users enter their HF_API_TOKEN in the UI
# Ready for Hugging Face Spaces
import time
import gradio as gr
from huggingface_hub import InferenceClient
# -----------------------
# Configuration
# -----------------------
GPT_MODEL_ID = "HuggingFaceH4/zephyr-7b-beta" # default model, can change
# -----------------------
# Chatbot Core
# -----------------------
def gpt_chat(prompt, history, thinking, thinking_time, hf_token):
"""
Generate a chat response using Hugging Face Inference API.
Users provide their own HF_API_TOKEN.
"""
if not hf_token or hf_token.strip() == "":
return history, "❌ Please enter your Hugging Face API Token."
if not prompt.strip():
return history, "Please write a message."
# Initialize client
try:
client = InferenceClient(model=GPT_MODEL_ID, token=hf_token)
except Exception as e:
return history, f"Error creating HF client: {str(e)}"
# Append user message to history
history = history + [(prompt, None)]
# Simulate thinking
if thinking:
time.sleep(max(0.0, thinking_time))
try:
# Build conversation prompt
conversation_text = ""
for user_msg, bot_msg in history:
if bot_msg is not None:
conversation_text += f"User: {user_msg}\nAssistant: {bot_msg}\n"
conversation_text += f"User: {prompt}\nAssistant:"
# Generate response
response = client.text_generation(
prompt=conversation_text,
max_new_tokens=200,
temperature=0.7,
do_sample=True
)
answer = response.replace(conversation_text, "").strip()
if "Assistant:" in answer:
answer = answer.split("Assistant:")[-1].strip()
# Update last entry in history
history = history[:-1] + [(prompt, answer)]
return history, "OK"
except Exception as e:
history = history[:-1] + [(prompt, "⚠ Error generating response.")]
return history, str(e)
# -----------------------
# Gradio UI
# -----------------------
with gr.Blocks(title="ThinkPad - Chatbot") as demo:
gr.Markdown("""
# 🤖 ThinkPad Chatbot
Enter your Hugging Face API Token below to start chatting.
Built with ❤️ by [JumpLander](https://jumplander.org)
""")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(label="Chat")
user_input = gr.Textbox(show_label=False, placeholder="Write a message and press Send")
hf_token_input = gr.Textbox(
label="Hugging Face API Token",
placeholder="Paste your HF_API_TOKEN here",
type="password"
)
thinking_mode = gr.Checkbox(label="Thinking mode", value=True)
thinking_time = gr.Slider(0, 5, value=2.0, step=0.5, label="Thinking time (seconds)")
status_box = gr.Textbox(label="Status")
send_btn = gr.Button("Send")
# handle send
def respond(history, msg, thinking, thinking_t, hf_token):
return gpt_chat(msg, history or [], thinking, thinking_t, hf_token)
send_btn.click(respond, inputs=[chatbot, user_input, thinking_mode, thinking_time, hf_token_input], outputs=[chatbot, status_box])
user_input.submit(respond, inputs=[chatbot, user_input, thinking_mode, thinking_time, hf_token_input], outputs=[chatbot, status_box])
if __name__ == "__main__":
demo.launch()
|