Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
username = os.getenv("YOUR_ID")
|
14 |
+
password = os.getenv("YOUR_PASSWORD")
|
15 |
+
|
16 |
+
mytitle = "<h1 align=center>RTL AI News Reader : Wat war lass am Land 🇱🇺 an op der Welt 🌎 ?</h1>"
|
17 |
+
|
18 |
+
mydescription="""
|
19 |
+
<h3 align='center'>Wat fir een Thema interesséiert Iech : 🐶 🏃🏻♂️ 🌗 🍇 🌈 🍽️ 🏆 🚘 ✈️ 🩺 </h3>
|
20 |
+
<table width=100%>
|
21 |
+
<tr>
|
22 |
+
<th width=50% bgcolor="Moccasin">Stell deng Froen op Lëtzebuergesch, oder an enger anerer Sprooch :</th>
|
23 |
+
<th bgcolor="Khaki">Äntwert vum OpenAI File-Search Assistent : </th>
|
24 |
+
</tr>
|
25 |
+
</table>
|
26 |
+
"""
|
27 |
+
|
28 |
+
myarticle ="""
|
29 |
+
<h3>Hannergrënn :</h3>
|
30 |
+
<p>Dës HuggingFace Space Demo gouf vum <a href="https://github.com/mbarnig">Marco Barnig</a> realiséiert.
|
31 |
+
Als kënstlech Intelligenz gëtt, mëttels API, den <a href="https://platform.openai.com/docs/models">OpenAI Modell</a>
|
32 |
+
gpt-4o-mini-2024-07-18 benotzt, deen als Kontext bis 128.000 Tokens ka benotzen, eng Äntwert op eng Fro vu maximal 16.384
|
33 |
+
Tokens ka ginn a bis zu 200.000 Tokens pro Minutt (TPM) ka beaarbechten.
|
34 |
+
De ganze lëtzebuergesche Contenu vun RTL.lu vum Ufank (2000, 2012, ???) bis September 2024 gouf a 50 JSON-Dateien opgespléckt an op
|
35 |
+
e Vector Store vum OpenAI File-Search Assistent "RTL News Reader" eropgelueden.
|
36 |
+
All Datei huet manner wéi 5 Milliounen Token, wat eng iewescht Grenz fir den AI Modell ass.
|
37 |
+
Et ass méiglech bis zu 10.000 Dateien op en OpenAI Assistent opzelueden.
|
38 |
+
D'Äntwerte vun de Beispiller sinn am Cache gespäichert a ginn duerfir ouni Delai ugewise.</p>
|
39 |
+
"""
|
40 |
+
|
41 |
+
myinput = gr.Textbox(lines=3, label="Wat wëllt Der wëssen ?")
|
42 |
+
|
43 |
+
myexamples = [
|
44 |
+
"Wat war lass am Juni 2023 ?",
|
45 |
+
"Wat ass gewosst iwwert de SREL ?",
|
46 |
+
"Wat fir eng Katastroph war 2022 zu Lëtzebuerg ?",
|
47 |
+
"Koumen an de leschte Jore gréisser Kriminalfäll viru Geriicht ?"
|
48 |
+
]
|
49 |
+
|
50 |
+
class EventHandler(AsyncAssistantEventHandler):
|
51 |
+
def __init__(self) -> None:
|
52 |
+
super().__init__()
|
53 |
+
self.response_text = ""
|
54 |
+
|
55 |
+
async def on_text_created(self, text) -> None:
|
56 |
+
self.response_text += str(text)
|
57 |
+
|
58 |
+
async def on_text_delta(self, delta, snapshot):
|
59 |
+
self.response_text += str(delta.value)
|
60 |
+
|
61 |
+
async def on_text_done(self, text):
|
62 |
+
pass
|
63 |
+
|
64 |
+
async def on_tool_call_created(self, tool_call):
|
65 |
+
self.response_text += f"\n[Tool Call]: {str(tool_call.type)}\n"
|
66 |
+
|
67 |
+
async def on_tool_call_delta(self, delta, snapshot):
|
68 |
+
if snapshot.id != getattr(self, "current_tool_call", None):
|
69 |
+
self.current_tool_call = snapshot.id
|
70 |
+
self.response_text += f"\n[Tool Call Delta]: {str(delta.type)}\n"
|
71 |
+
|
72 |
+
if delta.type == 'code_interpreter':
|
73 |
+
if delta.code_interpreter.input:
|
74 |
+
self.response_text += str(delta.code_interpreter.input)
|
75 |
+
if delta.code_interpreter.outputs:
|
76 |
+
self.response_text += "\n\n[Output]:\n"
|
77 |
+
for output in delta.code_interpreter.outputs:
|
78 |
+
if output.type == "logs":
|
79 |
+
self.response_text += f"\n{str(output.logs)}"
|
80 |
+
|
81 |
+
async def on_tool_call_done(self, text):
|
82 |
+
pass
|
83 |
+
|
84 |
+
# Initialize session variables
|
85 |
+
session_data = {"assistant_id": assistantID, "thread_id": None}
|
86 |
+
|
87 |
+
async def initialize_thread():
|
88 |
+
# Create a Thread
|
89 |
+
thread = await client.beta.threads.create()
|
90 |
+
# Store thread ID in session_data for later use
|
91 |
+
session_data["thread_id"] = thread.id
|
92 |
+
|
93 |
+
async def generate_response(user_input):
|
94 |
+
assistant_id = session_data["assistant_id"]
|
95 |
+
thread_id = session_data["thread_id"]
|
96 |
+
|
97 |
+
# Add a Message to the Thread
|
98 |
+
oai_message = await client.beta.threads.messages.create(
|
99 |
+
thread_id=thread_id,
|
100 |
+
role="user",
|
101 |
+
content=user_input
|
102 |
+
)
|
103 |
+
|
104 |
+
# Create and Stream a Run
|
105 |
+
event_handler = EventHandler()
|
106 |
+
|
107 |
+
async with client.beta.threads.runs.stream(
|
108 |
+
thread_id=thread_id,
|
109 |
+
assistant_id=assistant_id,
|
110 |
+
instructions="Please assist the user with their query.",
|
111 |
+
event_handler=event_handler,
|
112 |
+
) as stream:
|
113 |
+
# Yield incremental updates
|
114 |
+
async for _ in stream:
|
115 |
+
await asyncio.sleep(0.1) # Small delay to mimic streaming
|
116 |
+
yield event_handler.response_text
|
117 |
+
|
118 |
+
# Gradio interface function (generator)
|
119 |
+
async def gradio_chat_interface(user_input):
|
120 |
+
# Create a new event loop if none exists (or if we are in a new thread)
|
121 |
+
try:
|
122 |
+
loop = asyncio.get_running_loop()
|
123 |
+
except RuntimeError:
|
124 |
+
loop = asyncio.new_event_loop()
|
125 |
+
asyncio.set_event_loop(loop)
|
126 |
+
|
127 |
+
# Initialize the thread if not already done
|
128 |
+
if session_data["thread_id"] is None:
|
129 |
+
await initialize_thread()
|
130 |
+
|
131 |
+
# Generate and yield responses
|
132 |
+
async for response in generate_response(user_input):
|
133 |
+
yield response
|
134 |
+
|
135 |
+
# Set up Gradio interface with streaming
|
136 |
+
interface = gr.Interface(
|
137 |
+
fn=gradio_chat_interface,
|
138 |
+
inputs=myinput,
|
139 |
+
outputs="markdown",
|
140 |
+
title=mytitle,
|
141 |
+
description=mydescription,
|
142 |
+
article=myarticle,
|
143 |
+
live=False,
|
144 |
+
allow_flagging="never",
|
145 |
+
examples=myexamples
|
146 |
+
)
|
147 |
+
|
148 |
+
# Launch the Gradio app
|
149 |
+
interface.launch(auth=(username, password), auth_message="<h1>RTL AI News Reader</h1><p>Dëse HuggingFace Space ass e Prototyp an nach net zougänglech fir jiddereen. De Projet baséiert op engem OpenAI API File-Search Assistent a benotzt de Modell GPT-4o-mini. Interesséiert KI-Spezialiste kënnen eng ID a Passwuert beim [email protected] ufroen.</p>")
|