Spaces:
Running
Running
Kang Suhyun
suhyun.kang
commited on
[#29] Display responses simultaneously to prevent bias from generation speed (#44)
Browse files* [#29] Display responses simultaneously to prevent bias from generation speed
Changes:
- Fetch responses with disabled streaming
- It takes longer to display responses because the entire response is fetched
- Simulate streaming by slicing responses into chunks since it's the common UX pattern
* Update comment
---------
Co-authored-by: suhyun.kang <[email protected]>
- response.py +12 -49
response.py
CHANGED
@@ -27,18 +27,6 @@ def get_instruction(category, source_lang, target_lang):
|
|
27 |
return f"Translate the following text from {source_lang} to {target_lang}."
|
28 |
|
29 |
|
30 |
-
def response_generator(response: str):
|
31 |
-
for part in response:
|
32 |
-
content = part.choices[0].delta.content
|
33 |
-
if content is None:
|
34 |
-
continue
|
35 |
-
|
36 |
-
# To simulate a stream, we yield each character of the response.
|
37 |
-
for character in content:
|
38 |
-
yield character
|
39 |
-
|
40 |
-
|
41 |
-
# TODO(#29): Return results simultaneously to prevent bias from generation speed.
|
42 |
def get_responses(user_prompt, category, source_lang, target_lang):
|
43 |
if not category:
|
44 |
raise gr.Error("Please select a category.")
|
@@ -52,7 +40,7 @@ def get_responses(user_prompt, category, source_lang, target_lang):
|
|
52 |
activated_vote_buttons = [gr.Button(interactive=True) for _ in range(3)]
|
53 |
deactivated_vote_buttons = [gr.Button(interactive=False) for _ in range(3)]
|
54 |
|
55 |
-
|
56 |
for model in models:
|
57 |
try:
|
58 |
# TODO(#1): Allow user to set configuration.
|
@@ -63,48 +51,23 @@ def get_responses(user_prompt, category, source_lang, target_lang):
|
|
63 |
}, {
|
64 |
"content": user_prompt,
|
65 |
"role": "user"
|
66 |
-
}]
|
67 |
-
|
68 |
-
generators.append(response_generator(response))
|
69 |
|
70 |
# TODO(#1): Narrow down the exception type.
|
71 |
except Exception as e: # pylint: disable=broad-except
|
72 |
print(f"Error in bot_response: {e}")
|
73 |
raise e
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
if yielded is None:
|
86 |
-
continue
|
87 |
-
|
88 |
-
responses[i] += yielded
|
89 |
-
stop = False
|
90 |
-
|
91 |
-
# model_name_row and vote_row are hidden during response generation.
|
92 |
-
yield responses + models + deactivated_vote_buttons + [
|
93 |
-
instruction,
|
94 |
-
gr.Row(visible=False),
|
95 |
-
gr.Row(visible=False)
|
96 |
-
]
|
97 |
-
|
98 |
-
except StopIteration:
|
99 |
-
pass
|
100 |
-
|
101 |
-
# TODO(#1): Narrow down the exception type.
|
102 |
-
except Exception as e: # pylint: disable=broad-except
|
103 |
-
print(f"Error in generator: {e}")
|
104 |
-
raise e
|
105 |
-
|
106 |
-
if stop:
|
107 |
-
break
|
108 |
|
109 |
# After generating the response, the vote_row should become visible,
|
110 |
# while the model_name_row should remain hidden.
|
|
|
27 |
return f"Translate the following text from {source_lang} to {target_lang}."
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def get_responses(user_prompt, category, source_lang, target_lang):
|
31 |
if not category:
|
32 |
raise gr.Error("Please select a category.")
|
|
|
40 |
activated_vote_buttons = [gr.Button(interactive=True) for _ in range(3)]
|
41 |
deactivated_vote_buttons = [gr.Button(interactive=False) for _ in range(3)]
|
42 |
|
43 |
+
responses = []
|
44 |
for model in models:
|
45 |
try:
|
46 |
# TODO(#1): Allow user to set configuration.
|
|
|
51 |
}, {
|
52 |
"content": user_prompt,
|
53 |
"role": "user"
|
54 |
+
}])
|
55 |
+
responses.append(response.choices[0].message.content)
|
|
|
56 |
|
57 |
# TODO(#1): Narrow down the exception type.
|
58 |
except Exception as e: # pylint: disable=broad-except
|
59 |
print(f"Error in bot_response: {e}")
|
60 |
raise e
|
61 |
|
62 |
+
# It simulates concurrent stream response generation.
|
63 |
+
max_response_length = max(len(response) for response in responses)
|
64 |
+
for i in range(max_response_length):
|
65 |
+
yield [response[:i + 1] for response in responses
|
66 |
+
] + models + deactivated_vote_buttons + [
|
67 |
+
instruction,
|
68 |
+
gr.Row(visible=False),
|
69 |
+
gr.Row(visible=False)
|
70 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
# After generating the response, the vote_row should become visible,
|
73 |
# while the model_name_row should remain hidden.
|