Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import openai
|
@@ -59,9 +69,12 @@ async def respond(
|
|
59 |
return "Error occurred. Please try again."
|
60 |
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
65 |
return response
|
66 |
|
67 |
|
@@ -77,7 +90,7 @@ def launch_app():
|
|
77 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
78 |
response = gr.Text(label="Response")
|
79 |
|
80 |
-
# Use the
|
81 |
gr.Button("Generate Response").click(
|
82 |
generate_response,
|
83 |
inputs=[message, system_message, max_tokens, temperature, top_p],
|
@@ -90,4 +103,6 @@ def launch_app():
|
|
90 |
print("Please try again.")
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
-
launch_app()
|
|
|
|
|
|
1 |
+
The error message you're seeing is likely due to the way you're defining and calling the `generate_response` function in your Gradio app.
|
2 |
+
|
3 |
+
In Gradio, when you define a function with the `async` keyword, it's expected to be a coroutine that returns a value. However, when you define a function with the `async` keyword, you need to use the `await` keyword to call it.
|
4 |
+
|
5 |
+
In your case, you're defining the `generate_response` function as an `async` function, but you're not using the `await` keyword to call it. Instead, you're passing it as a callback to the `gr.Button` component.
|
6 |
+
|
7 |
+
To fix this issue, you need to define the `generate_response` function without the `async` keyword, and then use the `await` keyword to call the `respond` function inside it.
|
8 |
+
|
9 |
+
Here's an updated version of your code that should work:
|
10 |
+
```python
|
11 |
import gradio as gr
|
12 |
import os
|
13 |
import openai
|
|
|
69 |
return "Error occurred. Please try again."
|
70 |
|
71 |
|
72 |
+
# Gradio function to handle user input and response generation without history
|
73 |
+
def generate_response(message, system_message, max_tokens, temperature, top_p):
|
74 |
+
import asyncio
|
75 |
+
loop = asyncio.new_event_loop()
|
76 |
+
asyncio.set_event_loop(loop)
|
77 |
+
response = loop.run_until_complete(respond(message, system_message, max_tokens, temperature, top_p))
|
78 |
return response
|
79 |
|
80 |
|
|
|
90 |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
|
91 |
response = gr.Text(label="Response")
|
92 |
|
93 |
+
# Use the generate_response function without history
|
94 |
gr.Button("Generate Response").click(
|
95 |
generate_response,
|
96 |
inputs=[message, system_message, max_tokens, temperature, top_p],
|
|
|
103 |
print("Please try again.")
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
+
launch_app()
|
107 |
+
```
|
108 |
+
This code defines the `generate_response` function without the `async` keyword, and then uses the `await` keyword to call the `respond` function inside it. It also creates a new event loop to run the `respond` function, since it's an asynchronous function.
|