Spaces:
Runtime error
Runtime error
Initial Demo
Browse files- app.py +91 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import json
|
3 |
+
from httpx import AsyncClient, Timeout
|
4 |
+
|
5 |
+
CLIENT = AsyncClient(timeout=Timeout(5, read=7 * 60))
|
6 |
+
|
7 |
+
token = ""
|
8 |
+
|
9 |
+
async def stream_chat(
|
10 |
+
message: str,
|
11 |
+
simplification_level: float,
|
12 |
+
):
|
13 |
+
|
14 |
+
level_mapping = {4: "A1", 3: "A2", 2: "B1", 1: "B2"}
|
15 |
+
|
16 |
+
payload = {
|
17 |
+
"stream": True,
|
18 |
+
"inputText": message,
|
19 |
+
"maxNewTokens": 256,
|
20 |
+
"batchSize": 2,
|
21 |
+
"decodingStrategy": "beam_search",
|
22 |
+
"separateCompounds": True,
|
23 |
+
"filterComplexWords": True,
|
24 |
+
"simplificationLevel": level_mapping[int(simplification_level)]
|
25 |
+
}
|
26 |
+
|
27 |
+
headers = {
|
28 |
+
'accept': 'application/json',
|
29 |
+
'Authorization': f'Bearer {token}',
|
30 |
+
'Content-Type': 'application/json',
|
31 |
+
}
|
32 |
+
|
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"
|
37 |
+
async for chunk in response.aiter_text():
|
38 |
+
assert isinstance(chunk, str)
|
39 |
+
segments = chunk.split("}{")
|
40 |
+
for i in range(0, len(segments)):
|
41 |
+
if not segments[i].startswith("{"):
|
42 |
+
segments[i] = "{" + segments[i]
|
43 |
+
if not segments[i].endswith("}"):
|
44 |
+
segments[i] = segments[i] + "}"
|
45 |
+
parsed_objects = [json.loads(segment) for segment in segments]
|
46 |
+
for parsed_object in parsed_objects:
|
47 |
+
if 'result' in parsed_object:
|
48 |
+
cached = cached + parsed_object['result']
|
49 |
+
yield cached
|
50 |
+
|
51 |
+
|
52 |
+
# Gradio interface with custom CSS
|
53 |
+
with gr.Blocks(css="""
|
54 |
+
body {
|
55 |
+
background-color: white;
|
56 |
+
}
|
57 |
+
.gradio-container {
|
58 |
+
background-color: white;
|
59 |
+
}
|
60 |
+
.gr-input, .gr-slider {
|
61 |
+
background-color: #E6E6FA; /* Lilac background for textboxes and slider */
|
62 |
+
color: black;
|
63 |
+
border-radius: 10px;
|
64 |
+
padding: 10px;
|
65 |
+
}
|
66 |
+
.gr-button {
|
67 |
+
background-color: #9370DB; /* Dark lilac button */
|
68 |
+
color: white;
|
69 |
+
border-radius: 5px;
|
70 |
+
padding: 10px 20px;
|
71 |
+
}
|
72 |
+
""") as demo:
|
73 |
+
|
74 |
+
with gr.Row():
|
75 |
+
with gr.Column(scale=1):
|
76 |
+
input_box = gr.Textbox(label="Texteingabe-Feld", placeholder="Hier ihren Text bitte einfügen :)", lines=20)
|
77 |
+
slider = gr.Slider(label="Slider", minimum=1.0, maximum=4.0, step=1, value=1.0)
|
78 |
+
send_button = gr.Button("Send")
|
79 |
+
with gr.Column(scale=1):
|
80 |
+
output_box = gr.Textbox(label="Vereinfachung", lines=20, visible=True)
|
81 |
+
|
82 |
+
# Handle the button click with async streaming
|
83 |
+
send_button.click(
|
84 |
+
fn=stream_chat,
|
85 |
+
inputs=[input_box, slider],
|
86 |
+
outputs=output_box,
|
87 |
+
postprocess=lambda x: gr.update(visible=True) if x else gr.update(visible=False),
|
88 |
+
)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
httpx
|