Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| from config import backend_url | |
| import copy | |
| def conversation_with_bot(message, history): | |
| msg_payload = copy.deepcopy(history) | |
| msg_payload.append({"role":"user", "content":message}) | |
| payload = {"messages": msg_payload} | |
| # stream api | |
| with requests.post( | |
| f"{backend_url}/api/gemini/chat/stream", | |
| json=payload, | |
| stream=True | |
| ) as res: | |
| if res.status_code != 200: | |
| yield history + [ | |
| {"role": "user", "content": message}, | |
| {"role": "assistant", "content": f"Error: {res.text}"} | |
| ], "" | |
| return | |
| reply = "" | |
| for chunk in res.iter_content(chunk_size=None, decode_unicode=True): | |
| reply += chunk | |
| yield history +[ | |
| {"role": "user", "content": message}, | |
| {"role": "assistant", "content": reply} | |
| ], "" | |
| def create_conv_tab(): | |
| with gr.Tab("Conversation"): | |
| chatbot = gr.Chatbot( | |
| label="Conversation with Inori Chatbot", | |
| height=500, | |
| type="messages" | |
| ) | |
| msg = gr.Textbox(label="Your message", placeholder="Type here...") | |
| btn = gr.Button("Send") | |
| btn.click(conversation_with_bot, [msg, chatbot], [chatbot, msg]) | |
| msg.submit(conversation_with_bot, [msg, chatbot], [chatbot, msg]) |