import os import gradio as gr # Initialize the APIs cb_api_key = os.environ['CB_API_KEY'] cb_id = os.environ['CB_ID'] api_url = os.environ['CB_URL'] ########################################## import requests import json import uuid authorization_header = f'Bearer {cb_api_key}' headers = { 'Authorization': authorization_header, 'Content-Type': 'application/json' } ############################################ def read_chatbot_reply(message, conversation_id, data): data_this = data try: data_this["messages"].append({"content": message, "role": "user"}) data_this["conversationId"] = conversation_id data_this["chatId"] = cb_id response = requests.post(api_url, json=data_this, headers=headers, stream=False) response.raise_for_status() json_data = response.json() return json_data['text'] decoder = response.iter_content(chunk_size=None) for chunk in decoder: chunk_value = chunk.decode('utf-8') except requests.exceptions.RequestException as error: print('Error:', error) ############################################# def respond(message, chat_history1, conversation_id, data_state): data_state_this = {} if conversation_id != "": conversation_id_this=conversation_id data_state_this = data_state else: conversation_id_this = str(uuid.uuid4()) data_state_this["messages"] = [] data_state_this["conversationId"] = conversation_id_this bot_message = read_chatbot_reply(message, conversation_id_this, data_state_this) chat_history1_this = chat_history1 chat_history1_this.append((message, bot_message)) data_state_this["messages"].append({"content": bot_message, "role": "assistant"}) return "", chat_history1_this, chat_history1_this, conversation_id_this, data_state_this # Gradio Interface theme = gr.themes.Base( primary_hue="orange", ).set( body_text_size='*text_md', body_text_weight='300', background_fill_secondary='*neutral_200', border_color_primary='*neutral_400', color_accent='*primary_800', prose_header_text_weight='800', prose_text_size = '*text_md', block_label_text_size='*text_md', input_background_fill_focus='*secondary_800', input_text_weight='400', input_text_size='*text_lg', button_primary_text_color='*primary_950', button_shadow_hover='*shadow_drop_lg', ) with gr.Blocks(theme=theme, analytics_enabled= False) as demo: #Initialzustand der Buttons send_button_active = gr.State(True) textinput_active = gr.State(True) tie_button_active = gr.State(False) b_vote_button_active = gr.State(False) a_vote_button_active = gr.State(False) data_state = gr.State({"messages": [],"conversationId": ""}) conversation_id1 = gr.State("") chat_history1 = gr.State([]) # Initialisiert den Chat-Verlauf als leere Liste with gr.Row(): with gr.Column(scale=1): chatbox1 = gr.Chatbot(label="Chatbot", height=600, scale=1, value=[ (None, "Willkommen beim Familienratgeber Chat👋. Als virtueller Assistent kann ich Menschen mit Behinderungen und deren Angehörige beraten. Sie können mich zum Beispiel fragen:\n\n - Wie beantrage ich persönliche Assistenz?\n - Welche Vorteile bietet ein Schwerbehindertenausweis?\n - Welche Nachteilsausgleiche stehen mir bei einem GdB von 60 zu?")]) with gr.Column(scale=1): pass with gr.Row(): with gr.Column(scale=1): textinput = gr.Textbox(placeholder="Geben Sie hier Ihre Frage ein...", autofocus=True, show_label=False, visible=textinput_active) send_button = gr.Button("Absenden", variant="primary") send_button.click(respond, inputs=[textinput, chat_history1, conversation_id1, data_state],outputs=[textinput, chatbox1, chat_history1, conversation_id1, data_state]) textinput.submit(respond, inputs=[textinput, chat_history1, conversation_id1, data_state], outputs=[textinput, chatbox1, chat_history1, conversation_id1, data_state]) with gr.Column(scale=1): pass ##################################################### login_user = os.environ['user'] login_pw = os.environ['password'] demo.launch(auth = (login_user, login_pw), auth_message= "Bitte Nutzernamen und Passwort eingeben")