Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,56 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import openai
|
3 |
import gradio as gr
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Set your OpenAI API key from environment variable
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
|
12 |
+
# Retrieve the existing Assistant
|
13 |
+
assistant_id = "asst_Fps5fdccFdhxF95G6iOAwIdQ"
|
14 |
+
assistant = openai.beta.assistants.retrieve(assistant_id)
|
15 |
+
|
16 |
+
# Initialize the Thread
|
17 |
+
thread = openai.beta.threads.create()
|
18 |
+
|
19 |
+
def chat(message, history):
|
20 |
+
# Add the user's message to the thread
|
21 |
+
openai.beta.threads.messages.create(
|
22 |
+
thread_id=thread.id,
|
23 |
+
role="user",
|
24 |
+
content=message
|
25 |
+
)
|
26 |
+
|
27 |
+
# Run the Assistant
|
28 |
+
run = openai.beta.threads.runs.create(
|
29 |
+
thread_id=thread.id,
|
30 |
+
assistant_id=assistant.id
|
31 |
+
)
|
32 |
+
|
33 |
+
# Wait for the run to complete
|
34 |
+
while run.status != "completed":
|
35 |
+
run = openai.beta.threads.runs.retrieve(
|
36 |
+
thread_id=thread.id,
|
37 |
+
run_id=run.id
|
38 |
+
)
|
39 |
+
|
40 |
+
# Retrieve the assistant's messages
|
41 |
+
messages = openai.beta.threads.messages.list(thread_id=thread.id)
|
42 |
+
|
43 |
+
# Get the last message from the assistant
|
44 |
+
last_message = next(msg for msg in messages if msg.role == "assistant")
|
45 |
+
|
46 |
+
return last_message.content[0].text.value
|
47 |
+
|
48 |
+
# Create the Gradio interface
|
49 |
+
iface = gr.ChatInterface(
|
50 |
+
fn=chat,
|
51 |
+
title="Colossal Cave",
|
52 |
+
description="Ask me anything about Colossal Cave Adventure!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
)
|
54 |
|
55 |
+
# Launch the interface
|
56 |
+
iface.launch()
|
|