Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium" | |
| headers = {"Authorization": f"Bearer {HF_TOKEN}"} | |
| # ---------- AI call ---------- | |
| def query_hf(message, history): | |
| payload = { | |
| "inputs": message, | |
| "parameters": {"max_new_tokens": 120} | |
| } | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| data = response.json() | |
| if isinstance(data, list): | |
| return data[0]["generated_text"] | |
| return "Sorry, something went wrong." | |
| # ---------- Chat function ---------- | |
| def chat(message, history): | |
| bot_reply = query_hf(message, history) | |
| history.append((message, bot_reply)) | |
| return "", history | |
| def clear_chat(): | |
| return [] | |
| # ---------- UI ---------- | |
| with gr.Blocks(css="style.css", theme=gr.themes.Soft()) as demo: | |
| gr.HTML("<h2 class='logo'>π¬ Your ChatUI App</h2>") | |
| with gr.Row(elem_classes="main-container"): | |
| # Sidebar | |
| with gr.Column(scale=1, elem_classes="sidebar"): | |
| gr.Markdown("### Today") | |
| new_chat = gr.Button("β New Chat") | |
| clear = gr.Button("π Clear") | |
| gr.Markdown("---") | |
| gr.Markdown("Theme\nSettings") | |
| # Chat area | |
| with gr.Column(scale=4, elem_classes="chat-area"): | |
| chatbot = gr.Chatbot(height=520, elem_classes="chatbox") | |
| msg = gr.Textbox( | |
| placeholder="Type a message...", | |
| container=False, | |
| elem_classes="textbox" | |
| ) | |
| with gr.Row(): | |
| send = gr.Button("Send", variant="primary") | |
| retry = gr.Button("Retry") | |
| # ---------- Actions ---------- | |
| send.click(chat, [msg, chatbot], [msg, chatbot]) | |
| msg.submit(chat, [msg, chatbot], [msg, chatbot]) | |
| clear.click(clear_chat, None, chatbot) | |
| new_chat.click(clear_chat, None, chatbot) | |
| demo.launch() | |