| import gradio as gr |
| from g4f.Provider import Blackbox |
| from g4f import ChatCompletion |
| import g4f |
|
|
| |
| def create_chat(): |
| return [] |
|
|
| def chat_with_claude(message, history): |
| try: |
| |
| messages = [] |
| for human, assistant in history: |
| messages.append({"role": "user", "content": human}) |
| messages.append({"role": "assistant", "content": assistant}) |
| messages.append({"role": "user", "content": message}) |
| |
| |
| response = ChatCompletion.create( |
| model="claude-3.5-sonnet", |
| provider=Blackbox, |
| messages=messages, |
| stream=False |
| ) |
| |
| return str(response) |
| |
| except Exception as e: |
| return f"Произошла ошибка: {str(e)}" |
|
|
| |
| def create_interface(): |
| with gr.Blocks(css="footer {display: none}") as demo: |
| gr.Markdown(""" |
| # Чат с Claude AI |
| Введите ваше сообщение и получите ответ от AI ассистента. |
| """) |
| |
| chatbot = gr.Chatbot( |
| height=600, |
| show_label=False, |
| container=True, |
| bubble_full_width=False, |
| ) |
| |
| with gr.Row(): |
| txt = gr.Textbox( |
| scale=4, |
| show_label=False, |
| placeholder="Введите ваше сообщение здесь...", |
| container=False, |
| ) |
| submit_btn = gr.Button("Отправить", scale=1) |
| clear_btn = gr.Button("Очистить", scale=1) |
|
|
| txt.submit( |
| chat_with_claude, |
| [txt, chatbot], |
| [chatbot], |
| clear_input=True, |
| ) |
| |
| submit_btn.click( |
| chat_with_claude, |
| [txt, chatbot], |
| [chatbot], |
| clear_input=True, |
| ) |
| |
| clear_btn.click(lambda: None, None, chatbot, queue=False) |
| |
| return demo |
|
|
| |
| if __name__ == "__main__": |
| demo = create_interface() |
| demo.launch( |
| server_name="0.0.0.0", |
| server_port=7860, |
| share=True, |
| ) |