gAIdio / app.py
EmmaKW's picture
Update app.py
1cd6c64 verified
raw
history blame contribute delete
No virus
1.47 kB
import gradio as gr
import os
from openai import OpenAI
import time
read_key = os.environ.get('passw_gaidio', None)
assistant_id = os.environ.get('asst_id', None)
client = OpenAI(organization=os.environ.get('AI_org', None), api_key=os.environ.get('AI_key', None))
# Create a thread
thread = client.beta.threads.create()
print(thread.id)
def main(query,history):
# Add a message to a Thread
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=query
)
# Run the assistant
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant_id,
)
while True:
time.sleep(3)
run_status = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id
)
if run_status.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
response = ""
for msg in messages.data:
role = msg.role
content = msg.content[0].text.value
response += f"{role.capitalize()}: {content}\n\n"
break
return response+ "\n\n"
else:
continue
if __name__ == "__main__":
iface = gr.ChatInterface(fn=main, title="gAIdio (Proof of concept)", description="Fråga mig om uppgiftskrav!").launch(auth=("user", read_key))