| |
| import gradio as gr |
| from g4f.client import Client |
|
|
| client = Client() |
|
|
| |
| def respond(message, history): |
| |
| messages = [{"role": "system", "content": """You are a Web‑assistant. For every user request return **exactly one JSON object** |
| with the following possible fields: |
| { |
| "TEXT": "<optional short explanation>", |
| "WEBSITE": "<full URL to open>", |
| "SEARCH": "<search query>", |
| "SUGGESTIONS": [ |
| {"title":"...", "url":"..."}, |
| {"title":"...", "url":"..."} |
| ], |
| "TOOL": { |
| "action":"click|type|scroll|none", |
| "selector":"CSS selector (optional)", |
| "value":"text to type (if action==type)" |
| } |
| } |
| If you don't need any action, set all fields to null or empty strings. |
| """}] |
| |
| for human, assistant in history: |
| messages.append({"role": "user", "content": human}) |
| messages.append({"role": "assistant", "content": assistant}) |
| |
| messages.append({"role": "user", "content": message}) |
|
|
| try: |
| response = client.chat.completions.create( |
| model="deepseek-v3", |
| messages=messages |
| ) |
| bot_message = response.choices[0].message.content |
| except Exception as e: |
| bot_message = f"Ошибка: {str(e)}" |
|
|
| |
| return history + [[message, bot_message]] |
|
|
| |
| with gr.Blocks(title="ESP Brain") as demo: |
| gr.Markdown("## For api") |
|
|
| chatbot = gr.Chatbot( |
| height=600, |
| ) |
|
|
| with gr.Row(): |
| txt = gr.Textbox( |
| placeholder="Напиши сообщение...", |
| show_label=False, |
| scale=8 |
| ) |
| submit_btn = gr.Button("Отправить", scale=2) |
|
|
| with gr.Row(): |
| retry_btn = gr.Button("🔄 Повторить") |
| undo_btn = gr.Button("↩️ Отменить") |
| clear_btn = gr.Button("🗑️ Очистить") |
|
|
| |
| txt.submit(fn=respond, inputs=[txt, chatbot], outputs=chatbot) |
| submit_btn.click(fn=respond, inputs=[txt, chatbot], outputs=chatbot) |
|
|
| def retry_last(history): |
| if history: |
| last_user_msg = history[-1][0] |
| return history[:-1] + [[last_user_msg, None]] |
| return history |
|
|
| retry_btn.click(fn=retry_last, inputs=chatbot, outputs=chatbot, queue=False) |
|
|
| def undo_last(history): |
| return history[:-1] |
|
|
| undo_btn.click(fn=undo_last, inputs=chatbot, outputs=chatbot, queue=False) |
|
|
| clear_btn.click(lambda: [], outputs=chatbot, queue=False) |
|
|
| |
| if __name__ == "__main__": |
| demo.queue() |
| demo.launch( |
| share=True, |
| ssr_mode=False, |
| debug=True |
| ) |