File size: 1,466 Bytes
e9eee4b 2461c37 e9eee4b 1cd6c64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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))
|