Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,12 +5,108 @@ import asyncio
|
|
5 |
import os
|
6 |
|
7 |
# set the keys
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def myFunction(mode, password, prompt, exemples):
|
16 |
# do nothing
|
|
|
5 |
import os
|
6 |
|
7 |
# set the keys
|
8 |
+
client = AsyncOpenAI(
|
9 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
10 |
+
)
|
11 |
+
assistantID = os.getenv("OPENAI_ASSISTANT_ID")
|
12 |
+
mypassword = os.getenv("RTL_PASSWORD")
|
13 |
|
14 |
+
class EventHandler(AsyncAssistantEventHandler):
|
15 |
+
def __init__(self) -> None:
|
16 |
+
super().__init__()
|
17 |
+
self.response_text = ""
|
18 |
+
|
19 |
+
async def on_text_created(self, text) -> None:
|
20 |
+
self.response_text += str(text)
|
21 |
+
|
22 |
+
async def on_text_delta(self, delta, snapshot):
|
23 |
+
self.response_text += str(delta.value)
|
24 |
+
|
25 |
+
async def on_text_done(self, text):
|
26 |
+
pass
|
27 |
+
|
28 |
+
async def on_tool_call_created(self, tool_call):
|
29 |
+
self.response_text += f"\n[Tool Call]: {str(tool_call.type)}\n"
|
30 |
+
|
31 |
+
async def on_tool_call_delta(self, delta, snapshot):
|
32 |
+
if snapshot.id != getattr(self, "current_tool_call", None):
|
33 |
+
self.current_tool_call = snapshot.id
|
34 |
+
self.response_text += f"\n[Tool Call Delta]: {str(delta.type)}\n"
|
35 |
+
|
36 |
+
if delta.type == 'code_interpreter':
|
37 |
+
if delta.code_interpreter.input:
|
38 |
+
self.response_text += str(delta.code_interpreter.input)
|
39 |
+
if delta.code_interpreter.outputs:
|
40 |
+
self.response_text += "\n\n[Output]:\n"
|
41 |
+
for output in delta.code_interpreter.outputs:
|
42 |
+
if output.type == "logs":
|
43 |
+
self.response_text += f"\n{str(output.logs)}"
|
44 |
+
|
45 |
+
async def on_tool_call_done(self, text):
|
46 |
+
pass
|
47 |
+
|
48 |
+
# Initialize session variables
|
49 |
+
session_data = {"assistant_id": assistantID, "thread_id": None}
|
50 |
+
|
51 |
+
async def initialize_thread():
|
52 |
+
# Create a Thread
|
53 |
+
thread = await client.beta.threads.create()
|
54 |
+
# Store thread ID in session_data for later use
|
55 |
+
session_data["thread_id"] = thread.id
|
56 |
+
|
57 |
+
async def generate_response(user_input):
|
58 |
+
assistant_id = session_data["assistant_id"]
|
59 |
+
thread_id = session_data["thread_id"]
|
60 |
+
|
61 |
+
# Add a Message to the Thread
|
62 |
+
oai_message = await client.beta.threads.messages.create(
|
63 |
+
thread_id=thread_id,
|
64 |
+
role="user",
|
65 |
+
content=user_input
|
66 |
+
)
|
67 |
+
|
68 |
+
# Create and Stream a Run
|
69 |
+
event_handler = EventHandler()
|
70 |
+
|
71 |
+
async with client.beta.threads.runs.stream(
|
72 |
+
thread_id=thread_id,
|
73 |
+
assistant_id=assistant_id,
|
74 |
+
instructions="Please assist the user with their query.",
|
75 |
+
event_handler=event_handler,
|
76 |
+
) as stream:
|
77 |
+
# Yield incremental updates
|
78 |
+
async for _ in stream:
|
79 |
+
await asyncio.sleep(0.1) # Small delay to mimic streaming
|
80 |
+
yield event_handler.response_text
|
81 |
+
|
82 |
+
# Gradio interface function (generator)
|
83 |
+
async def gradio_chat_interface(mode, password, user_input, example):
|
84 |
+
if mode == "Beispiller":
|
85 |
+
filename = example[-6:-2] + ".md"
|
86 |
+
file = open("examples/" + filename, "r")
|
87 |
+
output = file.read()
|
88 |
+
yield output
|
89 |
+
else:
|
90 |
+
# check the password
|
91 |
+
if password == "":
|
92 |
+
yield "Veuillez entrer le mot de passe pour faire des recherches !"
|
93 |
+
elif password != mypassword:
|
94 |
+
yield "Veuillez entre le mot de passe correct pour faire des recherches !"
|
95 |
+
else:
|
96 |
+
# Create a new event loop if none exists (or if we are in a new thread)
|
97 |
+
try:
|
98 |
+
loop = asyncio.get_running_loop()
|
99 |
+
except RuntimeError:
|
100 |
+
loop = asyncio.new_event_loop()
|
101 |
+
asyncio.set_event_loop(loop)
|
102 |
+
|
103 |
+
# Initialize the thread if not already done
|
104 |
+
if session_data["thread_id"] is None:
|
105 |
+
await initialize_thread()
|
106 |
+
|
107 |
+
# Generate and yield responses
|
108 |
+
async for response in generate_response(user_input):
|
109 |
+
yield response
|
110 |
|
111 |
def myFunction(mode, password, prompt, exemples):
|
112 |
# do nothing
|