import gradio as gr
from huggingface_hub import InferenceClient
# Initialize the InferenceClient
client = InferenceClient()
with gr.Blocks(theme="ocean") as demo:
gr.HTML("""
Watermark Your Chats🔒💬
Append the Watermark text "-- AI Generated --" to the end of messages copied from the chatbot. You can set this to any useful value.
This helps indicate that the message was generated by an AI model.""")
with gr.Row():
chatbot = gr.Chatbot(type="messages",
label="openai/gpt-oss-20b",
watermark="-- AI Generated --",
show_copy_button=True,
show_copy_all_button=True)
gr.HTML("""
⬅️ Copy the text from the chatbot using the copy button ⿻ below the message
And then Paste it into the Clipboard below ⬇️
""")
with gr.Row():
with gr.Column():
msg = gr.Textbox(label="User Input", placeholder="Type your message here, then press enter.")
with gr.Column():
clipboard_textbox = gr.TextArea(label="Clipboard", placeholder="Paste the copied text here!" ,interactive=True)
with gr.Row():
clear = gr.ClearButton([msg, chatbot, clipboard_textbox], value="Clear")
with gr.Row():
gr.HTML('Space based on earlier chatbot watermarking demo from Yuvraj Sharma.')
def respond(message, chat_history):
try:
# Build messages list from chat history
messages = []
# Add all previous messages
for msg in chat_history:
messages.append({"role": msg["role"], "content": msg["content"]})
# Add the new user message
messages.append({"role": "user", "content": message})
# Get response from the model
response = client.chat_completion(
messages=messages,
model="openai/gpt-oss-20b",
max_tokens=500,
temperature=0.7,
)
# Extract the assistant's response
bot_message = response.choices[0].message.content
except Exception as e:
# Check if it's a quota/rate limit error
error_msg = str(e).lower()
if "quota" in error_msg or "rate limit" in error_msg or "429" in error_msg or "insufficient" in error_msg:
bot_message = "⚠️ The inference quota has been exhausted. Kindly duplicate this space and configure your HF_TOKEN to continue using the app."
else:
# For other errors, still provide a helpful message
bot_message = f"⚠️ An error occurred: {str(e)}. If this persists, please duplicate the space and configure your HF_TOKEN."
# Update chat history
chat_history.append({"role": "user", "content": message})
chat_history.append({"role": "assistant", "content": bot_message})
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
if __name__ == "__main__":
demo.launch(debug=False)