Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import AsyncAssistantEventHandler
|
| 2 |
+
from openai import AsyncOpenAI
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import asyncio
|
| 5 |
+
|
| 6 |
+
# Set your OpenAI API key here
|
| 7 |
+
client = AsyncOpenAI(
|
| 8 |
+
api_key="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"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
assistantID = "asst_pMk1lyBSaVZPulq44RvIJUNe"
|
| 12 |
+
|
| 13 |
+
class EventHandler(AsyncAssistantEventHandler):
|
| 14 |
+
def __init__(self) -> None:
|
| 15 |
+
super().__init__()
|
| 16 |
+
self.response_text = ""
|
| 17 |
+
|
| 18 |
+
async def on_text_created(self, text) -> None:
|
| 19 |
+
self.response_text += str(text)
|
| 20 |
+
|
| 21 |
+
async def on_text_delta(self, delta, snapshot):
|
| 22 |
+
self.response_text += str(delta.value)
|
| 23 |
+
|
| 24 |
+
async def on_text_done(self, text):
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
async def on_tool_call_created(self, tool_call):
|
| 28 |
+
self.response_text += f"\n[Tool Call]: {str(tool_call.type)}\n"
|
| 29 |
+
|
| 30 |
+
async def on_tool_call_delta(self, delta, snapshot):
|
| 31 |
+
if snapshot.id != getattr(self, "current_tool_call", None):
|
| 32 |
+
self.current_tool_call = snapshot.id
|
| 33 |
+
self.response_text += f"\n[Tool Call Delta]: {str(delta.type)}\n"
|
| 34 |
+
|
| 35 |
+
if delta.type == 'code_interpreter':
|
| 36 |
+
if delta.code_interpreter.input:
|
| 37 |
+
self.response_text += str(delta.code_interpreter.input)
|
| 38 |
+
if delta.code_interpreter.outputs:
|
| 39 |
+
self.response_text += "\n\n[Output]:\n"
|
| 40 |
+
for output in delta.code_interpreter.outputs:
|
| 41 |
+
if output.type == "logs":
|
| 42 |
+
self.response_text += f"\n{str(output.logs)}"
|
| 43 |
+
|
| 44 |
+
async def on_tool_call_done(self, text):
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
# Initialize session variables
|
| 48 |
+
session_data = {"assistant_id": assistantID, "thread_id": None}
|
| 49 |
+
|
| 50 |
+
async def initialize_thread():
|
| 51 |
+
# Create a Thread
|
| 52 |
+
thread = await client.beta.threads.create()
|
| 53 |
+
# Store thread ID in session_data for later use
|
| 54 |
+
session_data["thread_id"] = thread.id
|
| 55 |
+
|
| 56 |
+
async def generate_response(user_input):
|
| 57 |
+
assistant_id = session_data["assistant_id"]
|
| 58 |
+
thread_id = session_data["thread_id"]
|
| 59 |
+
|
| 60 |
+
# Add a Message to the Thread
|
| 61 |
+
oai_message = await client.beta.threads.messages.create(
|
| 62 |
+
thread_id=thread_id,
|
| 63 |
+
role="user",
|
| 64 |
+
content=user_input
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# Create and Stream a Run
|
| 68 |
+
event_handler = EventHandler()
|
| 69 |
+
|
| 70 |
+
async with client.beta.threads.runs.stream(
|
| 71 |
+
thread_id=thread_id,
|
| 72 |
+
assistant_id=assistant_id,
|
| 73 |
+
instructions="""You are a Code Interpreter to analyze JSON files with RTL comments. Here is the format of the files :
|
| 74 |
+
[
|
| 75 |
+
{
|
| 76 |
+
"context_id": "",
|
| 77 |
+
"date_created": "",
|
| 78 |
+
"text": " ",
|
| 79 |
+
"user_id": "",
|
| 80 |
+
"referer": "",
|
| 81 |
+
"status": ",
|
| 82 |
+
"thumbs": [
|
| 83 |
+
{
|
| 84 |
+
"user_id": "",
|
| 85 |
+
"score": "up",
|
| 86 |
+
"date": ""
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"user_id": "",
|
| 90 |
+
"score": "down",
|
| 91 |
+
"date": ""
|
| 92 |
+
},
|
| 93 |
+
}
|
| 94 |
+
]
|
| 95 |
+
You will search dates ("date_created" of a comment and "date" of the related thumbs), calculate the total number of up scores and down scores. You will answer questions about "context_id", "text" and "referers".""",
|
| 96 |
+
event_handler=event_handler,
|
| 97 |
+
) as stream:
|
| 98 |
+
# Yield incremental updates
|
| 99 |
+
async for _ in stream:
|
| 100 |
+
await asyncio.sleep(0.1) # Small delay to mimic streaming
|
| 101 |
+
yield event_handler.response_text
|
| 102 |
+
|
| 103 |
+
# Gradio interface function (generator)
|
| 104 |
+
async def gradio_chat_interface(user_input):
|
| 105 |
+
# Create a new event loop if none exists (or if we are in a new thread)
|
| 106 |
+
try:
|
| 107 |
+
loop = asyncio.get_running_loop()
|
| 108 |
+
except RuntimeError:
|
| 109 |
+
loop = asyncio.new_event_loop()
|
| 110 |
+
asyncio.set_event_loop(loop)
|
| 111 |
+
|
| 112 |
+
# Initialize the thread if not already done
|
| 113 |
+
if session_data["thread_id"] is None:
|
| 114 |
+
await initialize_thread()
|
| 115 |
+
|
| 116 |
+
# Generate and yield responses
|
| 117 |
+
async for response in generate_response(user_input):
|
| 118 |
+
yield response
|
| 119 |
+
|
| 120 |
+
# Set up Gradio interface with streaming
|
| 121 |
+
interface = gr.Interface(
|
| 122 |
+
fn=gradio_chat_interface,
|
| 123 |
+
inputs="text",
|
| 124 |
+
outputs="markdown",
|
| 125 |
+
title="OpenAI Interpreter with Gradio",
|
| 126 |
+
description="Ask anything and get an AI-generated response in real-time.",
|
| 127 |
+
live=False, # Important to allow streaming-like behavior
|
| 128 |
+
allow_flagging="never"
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Launch the Gradio app
|
| 132 |
+
interface.launch()
|