Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import json | |
from httpx import AsyncClient, Timeout | |
CLIENT = AsyncClient(timeout=Timeout(5, read=7 * 60)) | |
TOKEN = os.getenv("TEXT2KNOWLEDGE_API_TOKEN") | |
SIMPLIFICATION_LEVELS = ["A1", "A2", "B1", "B2"] | |
async def stream_chat(message: str, simplification_level: str, separate_compounds: bool): | |
payload = { | |
"stream": True, | |
"inputText": message, | |
"maxNewTokens": 256, | |
"batchSize": 1, | |
"decodingStrategy": "beam_search", | |
"separateCompounds": separate_compounds, | |
"filterComplexWords": True, | |
"simplificationLevel": simplification_level, | |
} | |
headers = { | |
'accept': 'application/json', | |
'Authorization': f'Bearer {TOKEN}', | |
'Content-Type': 'application/json', | |
} | |
# check that message is not empty or contains only whitespaces and or linebreaks | |
if not message.strip(): | |
yield "Bitte geben Sie einen Text ein", gr.update(visible=True) | |
else: | |
cached = "" | |
async with CLIENT.stream("POST", "https://middleware-api.text2knowledge.de/simplify", json=payload, headers=headers, timeout=None) as response: | |
if response.status_code != 200: | |
yield "Die Verarbeitung war nicht ok", gr.update(visible=True) | |
async for chunk in response.aiter_text(): | |
assert isinstance(chunk, str) | |
segments = chunk.split("}{") | |
for i in range(0, len(segments)): | |
if not segments[i].startswith("{"): | |
segments[i] = "{" + segments[i] | |
if not segments[i].endswith("}"): | |
segments[i] = segments[i] + "}" | |
parsed_objects = [json.loads(segment) for segment in segments] | |
latest = "" | |
for parsed_object in parsed_objects: | |
if 'result' in parsed_object: | |
if latest.endswith("\n") and parsed_object['result'].startswith(" "): | |
parsed_object['result'] = parsed_object['result'][1:] | |
latest = parsed_object['result'] | |
cached = cached + parsed_object['result'] | |
yield cached, gr.update(visible=False) | |
yield cached, gr.update(visible=True) | |
theme = gr.themes.Soft( | |
primary_hue="violet", | |
).set( | |
body_text_color_dark='*neutral_800', | |
background_fill_primary_dark='*neutral_50', | |
background_fill_secondary_dark='*neutral_50', | |
border_color_accent_dark='*primary_300', | |
border_color_primary_dark='*neutral_200', | |
color_accent_soft_dark='*primary_50', | |
link_text_color_dark='*secondary_600', | |
link_text_color_active_dark='*secondary_600', | |
link_text_color_hover_dark='*secondary_700', | |
link_text_color_visited_dark='*secondary_500', | |
code_background_fill_dark='*neutral_100', | |
block_background_fill_dark='white', | |
block_label_background_fill_dark='*primary_100', | |
block_label_text_color_dark='*primary_500', | |
block_title_text_color_dark='*primary_500', | |
checkbox_background_color_dark='*background_fill_primary', | |
checkbox_background_color_selected_dark='*primary_600', | |
checkbox_border_color_dark='*neutral_100', | |
checkbox_border_color_focus_dark='*primary_500', | |
checkbox_border_color_hover_dark='*neutral_300', | |
checkbox_border_color_selected_dark='*primary_600', | |
checkbox_border_width_dark='1px', | |
checkbox_label_background_fill_selected_dark='*primary_500', | |
checkbox_label_text_color_selected_dark='white', | |
error_background_fill_dark='#fef2f2', | |
error_border_color_dark='#b91c1c', | |
input_background_fill_dark='white', | |
input_background_fill_focus_dark='*secondary_500', | |
input_border_color_dark='*neutral_50', | |
input_border_color_focus_dark='*secondary_300', | |
input_placeholder_color_dark='*neutral_400', | |
slider_color_dark='*primary_500', | |
stat_background_fill_dark='*primary_300', | |
table_border_color_dark='*neutral_300', | |
table_even_background_fill_dark='white', | |
table_odd_background_fill_dark='*neutral_50', | |
button_primary_background_fill='*primary_600', | |
button_primary_background_fill_dark='*primary_600', | |
button_primary_background_fill_hover='*primary_500', | |
button_primary_border_color_dark='*primary_200', | |
button_secondary_background_fill='*primary_600', | |
button_secondary_background_fill_dark='*primary_600', | |
button_secondary_background_fill_hover='*primary_500', | |
button_secondary_border_color_dark='*neutral_200', | |
button_secondary_text_color='white' | |
) | |
with gr.Blocks(theme=theme, css="footer{display:none !important}", fill_height=True, fill_width=True) as demo: | |
gr.Markdown( | |
""" | |
<p align="center"> | |
<a href="https://text2knowledge.de/start]"> | |
<img src="https://text2knowledge.de/logo.svg" width="10%" height=10%> | |
</a> | |
</p> | |
""" | |
) | |
with gr.Row(): | |
with gr.Column(scale=3): | |
dropdown = gr.Dropdown( | |
label="Vereinfachungsniveau", | |
choices=SIMPLIFICATION_LEVELS, | |
value=SIMPLIFICATION_LEVELS[0], | |
interactive=True, | |
) | |
checkbox = gr.Checkbox( | |
label="Komposita trennen", | |
value=False, | |
) | |
with gr.Column(scale=97): | |
input_text = gr.Textbox( | |
label="Eingabetext", | |
lines=15, | |
value=None, | |
) | |
simplified_text = gr.Textbox( | |
label="Vereinfachter Text", | |
lines=15, | |
value=None, | |
visible=False, | |
) | |
simplify_button = gr.Button("Vereinfachen", visible=False) | |
clear_button = gr.Button("Löschen", visible=False) | |
def handle_input_change(input_val): | |
if input_val: | |
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False) # Show simplify, hide result & clear | |
else: | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) # Hide everything if input is empty | |
# Hide the simplify button and show the result and clear button when generation starts | |
def handle_button_click(): | |
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False) | |
# Clear function to reset input and result fields | |
def clear_fields(): | |
return "", gr.update(value="", visible=False), gr.update(visible=False) | |
input_text.change( | |
fn=handle_input_change, | |
inputs=[input_text], | |
outputs=[simplify_button, simplified_text, clear_button], | |
) | |
# Handle the button clicks: Hide the button and show the result field and clear button | |
simplify_button.click( | |
fn=stream_chat, | |
inputs=[input_text, dropdown, checkbox], | |
outputs=[simplified_text, clear_button], | |
) | |
# When button is clicked, hide it and show the result field and clear button | |
simplify_button.click( | |
fn=handle_button_click, | |
inputs=[], | |
outputs=[simplify_button, simplified_text, clear_button], | |
) | |
# Handle clear button click | |
clear_button.click( | |
fn=clear_fields, | |
inputs=[], | |
outputs=[input_text, simplified_text, clear_button], | |
) | |
if __name__ == "__main__": | |
demo.launch() | |