Download app.py from mrdatobg/hydraAI: direct link, hf CLI and curl.
- Browser
- Download file 2.95 kB
-
https://huggingface.co/spaces/mrdatobg/hydraAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/mrdatobg/hydraAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mrdatobg/hydraAI/resolve/main/app.py
2.95 kB
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # ===== USERS ===== | |
| users = {} | |
| current_user = {"name": None} | |
| def register(username, password): | |
| users[username] = password | |
| return "Registered ✔" | |
| def login(username, password): | |
| if username in users and users[username] == password: | |
| current_user["name"] = username | |
| return "Logged in ✔" | |
| return "Wrong login" | |
| # ===== AI ===== | |
| def ai_chat(message, history, mode, token): | |
| client = InferenceClient( | |
| token=token, | |
| model="openai/gpt-oss-20b" | |
| ) | |
| if mode == "Agent": | |
| system = "Build full HTML apps." | |
| elif mode == "Ask": | |
| system = "Answer shortly." | |
| else: | |
| system = "Helpful assistant." | |
| messages = [{"role": "system", "content": system}] | |
| for u, a in history: | |
| messages.append({"role": "user", "content": u}) | |
| messages.append({"role": "assistant", "content": a}) | |
| messages.append({"role": "user", "content": message}) | |
| response = "" | |
| for chunk in client.chat_completion( | |
| messages, | |
| stream=True): | |
| t = chunk.choices[0].delta.content or "" | |
| response += t | |
| preview_html = None | |
| source_code = None | |
| # Agent mode auto-build | |
| if mode == "Agent" and "<html" in response: | |
| preview_html = response | |
| source_code = response | |
| history.append((message, response)) | |
| return history, preview_html, source_code | |
| # ===== UI ===== | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🤖 SN AI Platform") | |
| # LOGIN | |
| with gr.Row(): | |
| with gr.Column(): | |
| username = gr.Textbox(label="Username") | |
| password = gr.Textbox( | |
| label="Password", | |
| type="password" | |
| ) | |
| login_status = gr.Textbox() | |
| gr.Button("Register").click( | |
| register, | |
| [username, password], | |
| login_status | |
| ) | |
| gr.Button("Login").click( | |
| login, | |
| [username, password], | |
| login_status | |
| ) | |
| with gr.Row(): | |
| # AI CHAT | |
| with gr.Column(): | |
| mode = gr.Radio( | |
| ["Default", "Ask", "Agent"], | |
| value="Default", | |
| label="AI Mode" | |
| ) | |
| token_box = gr.Textbox( | |
| label="HF Token" | |
| ) | |
| chat = gr.Chatbot() | |
| msg = gr.Textbox( | |
| label="Message" | |
| ) | |
| send = gr.Button("Send") | |
| # PREVIEW | |
| with gr.Column(): | |
| preview = gr.HTML( | |
| value="AI preview will appear here" | |
| ) | |
| source = gr.Code( | |
| language="html" | |
| ) | |
| send.click( | |
| ai_chat, | |
| inputs=[msg, chat, mode, token_box], | |
| outputs=[chat, preview, source] | |
| ) | |
| demo.launch() |