Spaces:
Runtime error
Runtime error
Disable demo
Browse files- app.py +4 -121
- old_app.py +189 -0
app.py
CHANGED
@@ -1,60 +1,4 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
import json
|
4 |
-
from httpx import AsyncClient, Timeout
|
5 |
-
|
6 |
-
CLIENT = AsyncClient(timeout=Timeout(5, read=7 * 60))
|
7 |
-
|
8 |
-
TOKEN = os.getenv("TEXT2KNOWLEDGE_API_TOKEN")
|
9 |
-
SIMPLIFICATION_LEVELS = ["A1", "A2", "B1", "B2"]
|
10 |
-
|
11 |
-
async def stream_chat(message: str, simplification_level: str, separate_compounds: bool):
|
12 |
-
payload = {
|
13 |
-
"stream": True,
|
14 |
-
"inputText": message,
|
15 |
-
"maxNewTokens": 256,
|
16 |
-
"batchSize": 1,
|
17 |
-
"decodingStrategy": "beam_search",
|
18 |
-
"separateCompounds": separate_compounds,
|
19 |
-
"filterComplexWords": True,
|
20 |
-
"simplificationLevel": simplification_level,
|
21 |
-
}
|
22 |
-
|
23 |
-
headers = {
|
24 |
-
'accept': 'application/json',
|
25 |
-
'Authorization': f'Bearer {TOKEN}',
|
26 |
-
'Content-Type': 'application/json',
|
27 |
-
}
|
28 |
-
|
29 |
-
# check that message is not empty or contains only whitespaces and or linebreaks
|
30 |
-
if not message.strip():
|
31 |
-
yield "Bitte geben Sie einen Text ein", gr.update(visible=True)
|
32 |
-
else:
|
33 |
-
cached = ""
|
34 |
-
async with CLIENT.stream("POST", "https://middleware-api.text2knowledge.de/simplify", json=payload, headers=headers, timeout=None) as response:
|
35 |
-
if response.status_code != 200:
|
36 |
-
yield "Die Verarbeitung war nicht ok", gr.update(visible=True)
|
37 |
-
|
38 |
-
async for chunk in response.aiter_text():
|
39 |
-
assert isinstance(chunk, str)
|
40 |
-
segments = chunk.split("}{")
|
41 |
-
for i in range(0, len(segments)):
|
42 |
-
if not segments[i].startswith("{"):
|
43 |
-
segments[i] = "{" + segments[i]
|
44 |
-
if not segments[i].endswith("}"):
|
45 |
-
segments[i] = segments[i] + "}"
|
46 |
-
parsed_objects = [json.loads(segment) for segment in segments]
|
47 |
-
latest = ""
|
48 |
-
for parsed_object in parsed_objects:
|
49 |
-
if 'result' in parsed_object:
|
50 |
-
if latest.endswith("\n") and parsed_object['result'].startswith(" "):
|
51 |
-
parsed_object['result'] = parsed_object['result'][1:]
|
52 |
-
latest = parsed_object['result']
|
53 |
-
cached = cached + parsed_object['result']
|
54 |
-
yield cached, gr.update(visible=False)
|
55 |
-
yield cached, gr.update(visible=True)
|
56 |
-
|
57 |
-
|
58 |
theme = gr.themes.Soft(
|
59 |
primary_hue="violet",
|
60 |
).set(
|
@@ -118,72 +62,11 @@ with gr.Blocks(theme=theme, css="footer{display:none !important}", fill_height=T
|
|
118 |
)
|
119 |
|
120 |
with gr.Row():
|
121 |
-
with gr.Column(scale=3):
|
122 |
-
dropdown = gr.Dropdown(
|
123 |
-
label="Vereinfachungsniveau",
|
124 |
-
choices=SIMPLIFICATION_LEVELS,
|
125 |
-
value=SIMPLIFICATION_LEVELS[0],
|
126 |
-
interactive=True,
|
127 |
-
)
|
128 |
-
checkbox = gr.Checkbox(
|
129 |
-
label="Komposita trennen",
|
130 |
-
value=False,
|
131 |
-
)
|
132 |
-
with gr.Column(scale=97):
|
133 |
-
input_text = gr.Textbox(
|
134 |
-
label="Eingabetext",
|
135 |
-
lines=15,
|
136 |
-
value=None,
|
137 |
-
)
|
138 |
-
simplified_text = gr.Textbox(
|
139 |
-
label="Vereinfachter Text",
|
140 |
-
lines=15,
|
141 |
-
value=None,
|
142 |
-
visible=False,
|
143 |
-
)
|
144 |
-
simplify_button = gr.Button("Vereinfachen", visible=False)
|
145 |
-
clear_button = gr.Button("Löschen", visible=False)
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) # Hide everything if input is empty
|
152 |
-
|
153 |
-
# Hide the simplify button and show the result and clear button when generation starts
|
154 |
-
def handle_button_click():
|
155 |
-
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
156 |
-
|
157 |
-
# Clear function to reset input and result fields
|
158 |
-
def clear_fields():
|
159 |
-
return "", gr.update(value="", visible=False), gr.update(visible=False)
|
160 |
-
|
161 |
-
input_text.change(
|
162 |
-
fn=handle_input_change,
|
163 |
-
inputs=[input_text],
|
164 |
-
outputs=[simplify_button, simplified_text, clear_button],
|
165 |
-
)
|
166 |
-
|
167 |
-
# Handle the button clicks: Hide the button and show the result field and clear button
|
168 |
-
simplify_button.click(
|
169 |
-
fn=stream_chat,
|
170 |
-
inputs=[input_text, dropdown, checkbox],
|
171 |
-
outputs=[simplified_text, clear_button],
|
172 |
-
)
|
173 |
-
|
174 |
-
# When button is clicked, hide it and show the result field and clear button
|
175 |
-
simplify_button.click(
|
176 |
-
fn=handle_button_click,
|
177 |
-
inputs=[],
|
178 |
-
outputs=[simplify_button, simplified_text, clear_button],
|
179 |
-
)
|
180 |
-
|
181 |
-
# Handle clear button click
|
182 |
-
clear_button.click(
|
183 |
-
fn=clear_fields,
|
184 |
-
inputs=[],
|
185 |
-
outputs=[input_text, simplified_text, clear_button],
|
186 |
-
)
|
187 |
|
188 |
if __name__ == "__main__":
|
189 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
theme = gr.themes.Soft(
|
3 |
primary_hue="violet",
|
4 |
).set(
|
|
|
62 |
)
|
63 |
|
64 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
with gr.Column():
|
67 |
+
gr.Markdown(
|
68 |
+
value="## Diese Demo wurde mit dem Ende der Smart Country Convention 2024 abgeschaltet.\n## Für eine personalisierte Demonstration schreibt eine E-Mail an info@text2knowledge.de oder erstellt euch ein Nutzerkonto bei https://portal.text2knowledge.de",
|
69 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
demo.launch()
|
old_app.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
from httpx import AsyncClient, Timeout
|
5 |
+
|
6 |
+
CLIENT = AsyncClient(timeout=Timeout(5, read=7 * 60))
|
7 |
+
|
8 |
+
TOKEN = os.getenv("TEXT2KNOWLEDGE_API_TOKEN")
|
9 |
+
SIMPLIFICATION_LEVELS = ["A1", "A2", "B1", "B2"]
|
10 |
+
|
11 |
+
async def stream_chat(message: str, simplification_level: str, separate_compounds: bool):
|
12 |
+
payload = {
|
13 |
+
"stream": True,
|
14 |
+
"inputText": message,
|
15 |
+
"maxNewTokens": 256,
|
16 |
+
"batchSize": 1,
|
17 |
+
"decodingStrategy": "beam_search",
|
18 |
+
"separateCompounds": separate_compounds,
|
19 |
+
"filterComplexWords": True,
|
20 |
+
"simplificationLevel": simplification_level,
|
21 |
+
}
|
22 |
+
|
23 |
+
headers = {
|
24 |
+
'accept': 'application/json',
|
25 |
+
'Authorization': f'Bearer {TOKEN}',
|
26 |
+
'Content-Type': 'application/json',
|
27 |
+
}
|
28 |
+
|
29 |
+
# check that message is not empty or contains only whitespaces and or linebreaks
|
30 |
+
if not message.strip():
|
31 |
+
yield "Bitte geben Sie einen Text ein", gr.update(visible=True)
|
32 |
+
else:
|
33 |
+
cached = ""
|
34 |
+
async with CLIENT.stream("POST", "https://middleware-api.text2knowledge.de/simplify", json=payload, headers=headers, timeout=None) as response:
|
35 |
+
if response.status_code != 200:
|
36 |
+
yield "Die Verarbeitung war nicht ok", gr.update(visible=True)
|
37 |
+
|
38 |
+
async for chunk in response.aiter_text():
|
39 |
+
assert isinstance(chunk, str)
|
40 |
+
segments = chunk.split("}{")
|
41 |
+
for i in range(0, len(segments)):
|
42 |
+
if not segments[i].startswith("{"):
|
43 |
+
segments[i] = "{" + segments[i]
|
44 |
+
if not segments[i].endswith("}"):
|
45 |
+
segments[i] = segments[i] + "}"
|
46 |
+
parsed_objects = [json.loads(segment) for segment in segments]
|
47 |
+
latest = ""
|
48 |
+
for parsed_object in parsed_objects:
|
49 |
+
if 'result' in parsed_object:
|
50 |
+
if latest.endswith("\n") and parsed_object['result'].startswith(" "):
|
51 |
+
parsed_object['result'] = parsed_object['result'][1:]
|
52 |
+
latest = parsed_object['result']
|
53 |
+
cached = cached + parsed_object['result']
|
54 |
+
yield cached, gr.update(visible=False)
|
55 |
+
yield cached, gr.update(visible=True)
|
56 |
+
|
57 |
+
|
58 |
+
theme = gr.themes.Soft(
|
59 |
+
primary_hue="violet",
|
60 |
+
).set(
|
61 |
+
body_text_color_dark='*neutral_800',
|
62 |
+
background_fill_primary_dark='*neutral_50',
|
63 |
+
background_fill_secondary_dark='*neutral_50',
|
64 |
+
border_color_accent_dark='*primary_300',
|
65 |
+
border_color_primary_dark='*neutral_200',
|
66 |
+
color_accent_soft_dark='*primary_50',
|
67 |
+
link_text_color_dark='*secondary_600',
|
68 |
+
link_text_color_active_dark='*secondary_600',
|
69 |
+
link_text_color_hover_dark='*secondary_700',
|
70 |
+
link_text_color_visited_dark='*secondary_500',
|
71 |
+
code_background_fill_dark='*neutral_100',
|
72 |
+
block_background_fill_dark='white',
|
73 |
+
block_label_background_fill_dark='*primary_100',
|
74 |
+
block_label_text_color_dark='*primary_500',
|
75 |
+
block_title_text_color_dark='*primary_500',
|
76 |
+
checkbox_background_color_dark='*background_fill_primary',
|
77 |
+
checkbox_background_color_selected_dark='*primary_600',
|
78 |
+
checkbox_border_color_dark='*neutral_100',
|
79 |
+
checkbox_border_color_focus_dark='*primary_500',
|
80 |
+
checkbox_border_color_hover_dark='*neutral_300',
|
81 |
+
checkbox_border_color_selected_dark='*primary_600',
|
82 |
+
checkbox_border_width_dark='1px',
|
83 |
+
checkbox_label_background_fill_selected_dark='*primary_500',
|
84 |
+
checkbox_label_text_color_selected_dark='white',
|
85 |
+
error_background_fill_dark='#fef2f2',
|
86 |
+
error_border_color_dark='#b91c1c',
|
87 |
+
input_background_fill_dark='white',
|
88 |
+
input_background_fill_focus_dark='*secondary_500',
|
89 |
+
input_border_color_dark='*neutral_50',
|
90 |
+
input_border_color_focus_dark='*secondary_300',
|
91 |
+
input_placeholder_color_dark='*neutral_400',
|
92 |
+
slider_color_dark='*primary_500',
|
93 |
+
stat_background_fill_dark='*primary_300',
|
94 |
+
table_border_color_dark='*neutral_300',
|
95 |
+
table_even_background_fill_dark='white',
|
96 |
+
table_odd_background_fill_dark='*neutral_50',
|
97 |
+
button_primary_background_fill='*primary_600',
|
98 |
+
button_primary_background_fill_dark='*primary_600',
|
99 |
+
button_primary_background_fill_hover='*primary_500',
|
100 |
+
button_primary_border_color_dark='*primary_200',
|
101 |
+
button_secondary_background_fill='*primary_600',
|
102 |
+
button_secondary_background_fill_dark='*primary_600',
|
103 |
+
button_secondary_background_fill_hover='*primary_500',
|
104 |
+
button_secondary_border_color_dark='*neutral_200',
|
105 |
+
button_secondary_text_color='white'
|
106 |
+
)
|
107 |
+
|
108 |
+
|
109 |
+
with gr.Blocks(theme=theme, css="footer{display:none !important}", fill_height=True, fill_width=True) as demo:
|
110 |
+
gr.Markdown(
|
111 |
+
"""
|
112 |
+
<p align="center">
|
113 |
+
<a href="https://text2knowledge.de/start]">
|
114 |
+
<img src="https://text2knowledge.de/logo.svg" width="10%" height=10%>
|
115 |
+
</a>
|
116 |
+
</p>
|
117 |
+
"""
|
118 |
+
)
|
119 |
+
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column(scale=3):
|
122 |
+
dropdown = gr.Dropdown(
|
123 |
+
label="Vereinfachungsniveau",
|
124 |
+
choices=SIMPLIFICATION_LEVELS,
|
125 |
+
value=SIMPLIFICATION_LEVELS[0],
|
126 |
+
interactive=True,
|
127 |
+
)
|
128 |
+
checkbox = gr.Checkbox(
|
129 |
+
label="Komposita trennen",
|
130 |
+
value=False,
|
131 |
+
)
|
132 |
+
with gr.Column(scale=97):
|
133 |
+
input_text = gr.Textbox(
|
134 |
+
label="Eingabetext",
|
135 |
+
lines=15,
|
136 |
+
value=None,
|
137 |
+
)
|
138 |
+
simplified_text = gr.Textbox(
|
139 |
+
label="Vereinfachter Text",
|
140 |
+
lines=15,
|
141 |
+
value=None,
|
142 |
+
visible=False,
|
143 |
+
)
|
144 |
+
simplify_button = gr.Button("Vereinfachen", visible=False)
|
145 |
+
clear_button = gr.Button("Löschen", visible=False)
|
146 |
+
|
147 |
+
def handle_input_change(input_val):
|
148 |
+
if input_val:
|
149 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False) # Show simplify, hide result & clear
|
150 |
+
else:
|
151 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) # Hide everything if input is empty
|
152 |
+
|
153 |
+
# Hide the simplify button and show the result and clear button when generation starts
|
154 |
+
def handle_button_click():
|
155 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
156 |
+
|
157 |
+
# Clear function to reset input and result fields
|
158 |
+
def clear_fields():
|
159 |
+
return "", gr.update(value="", visible=False), gr.update(visible=False)
|
160 |
+
|
161 |
+
input_text.change(
|
162 |
+
fn=handle_input_change,
|
163 |
+
inputs=[input_text],
|
164 |
+
outputs=[simplify_button, simplified_text, clear_button],
|
165 |
+
)
|
166 |
+
|
167 |
+
# Handle the button clicks: Hide the button and show the result field and clear button
|
168 |
+
simplify_button.click(
|
169 |
+
fn=stream_chat,
|
170 |
+
inputs=[input_text, dropdown, checkbox],
|
171 |
+
outputs=[simplified_text, clear_button],
|
172 |
+
)
|
173 |
+
|
174 |
+
# When button is clicked, hide it and show the result field and clear button
|
175 |
+
simplify_button.click(
|
176 |
+
fn=handle_button_click,
|
177 |
+
inputs=[],
|
178 |
+
outputs=[simplify_button, simplified_text, clear_button],
|
179 |
+
)
|
180 |
+
|
181 |
+
# Handle clear button click
|
182 |
+
clear_button.click(
|
183 |
+
fn=clear_fields,
|
184 |
+
inputs=[],
|
185 |
+
outputs=[input_text, simplified_text, clear_button],
|
186 |
+
)
|
187 |
+
|
188 |
+
if __name__ == "__main__":
|
189 |
+
demo.launch()
|