mbarnig commited on
Commit
4d8e484
Β·
verified Β·
1 Parent(s): 3d9450b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -0
app.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from openai import AsyncAssistantEventHandler
2
+ from openai import AsyncOpenAI
3
+ import gradio as gr
4
+ import asyncio
5
+ import os
6
+
7
+ # set the keys
8
+ client = AsyncOpenAI(
9
+ api_key=os.getenv("OPENAI_API_KEY")
10
+ )
11
+
12
+ assistantID = os.getenv("OPENAI_ASSISTANT_ID")
13
+ mypassword = os.getenv("RTL_PASSWORD")
14
+
15
+ class EventHandler(AsyncAssistantEventHandler):
16
+ def __init__(self) -> None:
17
+ super().__init__()
18
+ self.response_text = ""
19
+
20
+ async def on_text_created(self, text) -> None:
21
+ self.response_text += str(text)
22
+
23
+ async def on_text_delta(self, delta, snapshot):
24
+ self.response_text += str(delta.value)
25
+
26
+ async def on_text_done(self, text):
27
+ pass
28
+
29
+ async def on_tool_call_created(self, tool_call):
30
+ self.response_text += f"\n[Tool Call]: {str(tool_call.type)}\n"
31
+
32
+ async def on_tool_call_delta(self, delta, snapshot):
33
+ if snapshot.id != getattr(self, "current_tool_call", None):
34
+ self.current_tool_call = snapshot.id
35
+ self.response_text += f"\n[Tool Call Delta]: {str(delta.type)}\n"
36
+
37
+ if delta.type == 'code_interpreter':
38
+ if delta.code_interpreter.input:
39
+ self.response_text += str(delta.code_interpreter.input)
40
+ if delta.code_interpreter.outputs:
41
+ self.response_text += "\n\n[Output]:\n"
42
+ for output in delta.code_interpreter.outputs:
43
+ if output.type == "logs":
44
+ self.response_text += f"\n{str(output.logs)}"
45
+
46
+ async def on_tool_call_done(self, text):
47
+ pass
48
+
49
+ # Initialize session variables
50
+ session_data = {"assistant_id": assistantID, "thread_id": None}
51
+
52
+ async def initialize_thread():
53
+ # Create a Thread
54
+ thread = await client.beta.threads.create()
55
+ # Store thread ID in session_data for later use
56
+ session_data["thread_id"] = thread.id
57
+
58
+ async def generate_response(user_input):
59
+ if user_input == "":
60
+ yield "Submit your question as input !"
61
+ else:
62
+ assistant_id = session_data["assistant_id"]
63
+ thread_id = session_data["thread_id"]
64
+
65
+ # Add a Message to the Thread
66
+ oai_message = await client.beta.threads.messages.create(
67
+ thread_id=thread_id,
68
+ role="user",
69
+ content=user_input
70
+ )
71
+
72
+ # Create and Stream a Run
73
+ event_handler = EventHandler()
74
+
75
+ async with client.beta.threads.runs.stream(
76
+ thread_id=thread_id,
77
+ assistant_id=assistant_id,
78
+ instructions="Please assist the user with their query.",
79
+ event_handler=event_handler,
80
+ ) as stream:
81
+ # Yield incremental updates
82
+ async for _ in stream:
83
+ await asyncio.sleep(0.1) # Small delay to mimic streaming
84
+ yield event_handler.response_text
85
+
86
+ # Gradio interface function (generator)
87
+ async def gradio_chat_interface(mode, password, user_input, example):
88
+ if mode == "Examples":
89
+ filename = example[-6:-2] + ".md"
90
+ file = open("examples/" + filename, "r")
91
+ output = file.read()
92
+ yield output
93
+ else:
94
+ # check the password
95
+ if password == "":
96
+ yield "To search you need to enter an RTL password !"
97
+ elif password != mypassword:
98
+ yield "Please enter the correct RTL password !"
99
+ else:
100
+
101
+ # Create a new event loop if none exists (or if we are in a new thread)
102
+ try:
103
+ loop = asyncio.get_running_loop()
104
+ except RuntimeError:
105
+ loop = asyncio.new_event_loop()
106
+ asyncio.set_event_loop(loop)
107
+
108
+ # Initialize the thread if not already done
109
+ if session_data["thread_id"] is None:
110
+ await initialize_thread()
111
+
112
+ # Generate and yield responses
113
+ async for response in generate_response(user_input):
114
+ yield response
115
+
116
+ with gr.Blocks() as demo:
117
+ with gr.Row():
118
+ myTitle = gr.HTML("<h2 align=center>RTL Multilingual AI News Reader : What happened in the country πŸ‡±πŸ‡Ί or in the world 🌎 ?</h2>")
119
+ with gr.Row():
120
+ myDescription = gr.HTML("""
121
+ <h3 align='center'>What topic interests you ?</h3>
122
+ <p align='center'>🐢 πŸƒπŸ»β€β™‚οΈ πŸŒ— πŸ‡ 🌈 🍽️ πŸ† 🚘 ✈️ 🩺 </p>
123
+ <p align='center' bgcolor="Moccasin">Submit your question in english or in another language !</p>
124
+ """
125
+ )
126
+ with gr.Row():
127
+ mode = gr.Radio(choices=["Search", "Examples"], label = "You can run the examples without password !", value = "Examples")
128
+ pw = gr.Textbox(lines=1, label="Enter the RTL password : ")
129
+ with gr.Row():
130
+ question = gr.Textbox(lines=3, label="Please submit your question ?")
131
+ with gr.Row():
132
+ examples = gr.Radio(["What happened in May 2009 in Luxembourg ?", "What were the highlights in 2017 ?"], value="What happened in May 2009 in Luxembourg ?", label="Examples")
133
+ with gr.Row():
134
+ clear = gr.Button("Clear")
135
+ submit = gr.Button("Submit")
136
+ with gr.Row():
137
+ mySubtitle = gr.HTML("<p align='center' bgcolor='Khaki'>English RTL News :</p>")
138
+ with gr.Row():
139
+ myOutput = gr.Markdown(label="Answer from the OpenAI File-Search Assistent :")
140
+
141
+ submit.click(fn = gradio_chat_interface, inputs=[mode, pw, question, examples], outputs = myOutput)
142
+ demo.launch()