Upload emmessage.py
Browse files- emmessage.py +71 -0
emmessage.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Initialize clients with API keys
|
7 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "<your OpenAI API key if not set as env var>"))
|
8 |
+
|
9 |
+
# Step 1: Create an Assistant
|
10 |
+
assistant = client.beta.assistants.create(
|
11 |
+
name="WEA Emergency Message Generator",
|
12 |
+
instructions="You are an emergency message generator. You will write emergency messages of 360 characters or less to broadcast to wireless phones. Do not include hashtags or emojis.",
|
13 |
+
model="gpt-4-turbo-preview",
|
14 |
+
tools=[{"type": "retrieval"}]
|
15 |
+
)
|
16 |
+
|
17 |
+
# Step 2: Create a Thread
|
18 |
+
thread = client.beta.threads.create()
|
19 |
+
|
20 |
+
def main(query):
|
21 |
+
# Step 3: Add a Message to a Thread
|
22 |
+
message = client.beta.threads.messages.create(
|
23 |
+
thread_id=thread.id,
|
24 |
+
role="user",
|
25 |
+
content=query
|
26 |
+
)
|
27 |
+
|
28 |
+
# Step 4: Run the Assistant
|
29 |
+
run = client.beta.threads.runs.create(
|
30 |
+
thread_id=thread.id,
|
31 |
+
assistant_id=assistant.id,
|
32 |
+
instructions="You are an emergency message generator. You will write emergency messages of 360 characters or less to broadcast to wireless phones. Do not include hashtags or emojis."
|
33 |
+
)
|
34 |
+
|
35 |
+
while True:
|
36 |
+
# Wait for 5 seconds
|
37 |
+
time.sleep(5)
|
38 |
+
|
39 |
+
# Retrieve the run status
|
40 |
+
run_status = client.beta.threads.runs.retrieve(
|
41 |
+
thread_id=thread.id,
|
42 |
+
run_id=run.id
|
43 |
+
)
|
44 |
+
|
45 |
+
# If run is completed, get messages
|
46 |
+
if run_status.status == 'completed':
|
47 |
+
messages = client.beta.threads.messages.list(
|
48 |
+
thread_id=thread.id,
|
49 |
+
limit=1
|
50 |
+
)
|
51 |
+
response = ""
|
52 |
+
# Loop through messages and print content based on role
|
53 |
+
for msg in messages.data:
|
54 |
+
role = msg.role
|
55 |
+
content = msg.content[0].text.value
|
56 |
+
response += f"{role.capitalize()}: {content}\n\n"
|
57 |
+
return response+"\n\n"
|
58 |
+
else:
|
59 |
+
continue
|
60 |
+
|
61 |
+
# Create a Gradio Interface
|
62 |
+
iface = gr.Interface(
|
63 |
+
fn=main,
|
64 |
+
inputs=[gr.Textbox(label="Describe the warning here", lines=4)],
|
65 |
+
outputs=[gr.Textbox(label="Suggested Message", lines=4)],
|
66 |
+
allow_flagging="never",
|
67 |
+
title="WEA Emergency Message Assistant",
|
68 |
+
description="Hello. I'm Emmy your emergency messaging bot. Please describe the message you would like me to generate.",
|
69 |
+
article="All input and output will be saved for research purposes.").launch(share=True)
|
70 |
+
iface.launch()
|
71 |
+
|