Spaces:
Sleeping
Sleeping
naourpally
commited on
Commit
·
c78d430
1
Parent(s):
a51f836
Streaming the response
Browse files
app.py
CHANGED
@@ -8,10 +8,20 @@ def get_text_response(prompt):
|
|
8 |
"prompt": prompt,
|
9 |
"stream": True
|
10 |
}
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def clear_chat():
|
17 |
return "", ""
|
@@ -66,13 +76,14 @@ with gr.Blocks(css=css) as demo:
|
|
66 |
)
|
67 |
|
68 |
with gr.Row():
|
69 |
-
prompt = gr.Textbox(label="Enter your prompt")
|
70 |
submit_button = gr.Button("Submit")
|
71 |
clear_button = gr.Button("Clear")
|
72 |
-
output = gr.Textbox(label="Response")
|
73 |
|
74 |
-
submit_button.click(fn=get_text_response, inputs=prompt, outputs=output)
|
75 |
clear_button.click(fn=clear_chat, inputs=[], outputs=[prompt, output])
|
|
|
76 |
gr.HTML(
|
77 |
"""
|
78 |
<div class="acknowledgments">
|
@@ -81,4 +92,5 @@ with gr.Blocks(css=css) as demo:
|
|
81 |
"""
|
82 |
)
|
83 |
|
84 |
-
|
|
|
|
8 |
"prompt": prompt,
|
9 |
"stream": True
|
10 |
}
|
11 |
+
with requests.post(api_url, json=data_payload, stream=True) as response:
|
12 |
+
for line in response.iter_lines():
|
13 |
+
if line:
|
14 |
+
decoded_line = line.decode('utf-8') # Assuming the API sends responses in UTF-8 encoding
|
15 |
+
json_line = decoded_line.strip(',') # Remove trailing commas that might be present
|
16 |
+
try:
|
17 |
+
response_json = json.loads(json_line)
|
18 |
+
text_response = response_json.get("response", "")
|
19 |
+
done = response_json.get("done", False)
|
20 |
+
yield text_response
|
21 |
+
if done:
|
22 |
+
break
|
23 |
+
except json.JSONDecodeError as e:
|
24 |
+
continue # Handle incomplete JSON chunks or keep alive newlines
|
25 |
|
26 |
def clear_chat():
|
27 |
return "", ""
|
|
|
76 |
)
|
77 |
|
78 |
with gr.Row():
|
79 |
+
prompt = gr.Textbox(label="Enter your prompt", lines=5, placeholder="Type something...")
|
80 |
submit_button = gr.Button("Submit")
|
81 |
clear_button = gr.Button("Clear")
|
82 |
+
output = gr.Textbox(label="Response", lines=10, placeholder="Response will appear here...")
|
83 |
|
84 |
+
submit_button.click(fn=get_text_response, inputs=prompt, outputs=output, streaming=True)
|
85 |
clear_button.click(fn=clear_chat, inputs=[], outputs=[prompt, output])
|
86 |
+
|
87 |
gr.HTML(
|
88 |
"""
|
89 |
<div class="acknowledgments">
|
|
|
92 |
"""
|
93 |
)
|
94 |
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo.launch()
|