Spaces:
Runtime error
Runtime error
shigeru saito
commited on
Commit
·
db3f703
1
Parent(s):
abcad11
first commit
Browse files- .env.sample +2 -0
- .gitignore +1 -0
- app.py +87 -0
.env.sample
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
| 2 |
+
OPENAI_ASSISTANT_ID=asst_xxxxxxxxxxxxxxxxxxxxxxxx
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import codecs
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import openai
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
|
| 9 |
+
# OpenAI API キーの設定
|
| 10 |
+
load_dotenv()
|
| 11 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 12 |
+
assistant_id = os.getenv('OPENAI_ASSISTANT_ID')
|
| 13 |
+
|
| 14 |
+
import json
|
| 15 |
+
|
| 16 |
+
def assistant_response(prompt):
|
| 17 |
+
client = openai.OpenAI()
|
| 18 |
+
|
| 19 |
+
print("### Step 1: Get the Assistant's ID ###")
|
| 20 |
+
assistant = client.beta.assistants.retrieve(assistant_id)
|
| 21 |
+
print(assistant)
|
| 22 |
+
|
| 23 |
+
"### Step 2: Create a Thread ###"
|
| 24 |
+
empty_thread = client.beta.threads.create()
|
| 25 |
+
thread_id = empty_thread.id
|
| 26 |
+
print(empty_thread)
|
| 27 |
+
|
| 28 |
+
print("### Step 3: Add a Message to the Thread ###")
|
| 29 |
+
thread = client.beta.threads.retrieve(thread_id)
|
| 30 |
+
print(thread)
|
| 31 |
+
|
| 32 |
+
print("### Step 4: Add a Message to the Thread ###")
|
| 33 |
+
thread_message = client.beta.threads.messages.create(
|
| 34 |
+
thread_id,
|
| 35 |
+
role="user",
|
| 36 |
+
content=prompt,
|
| 37 |
+
)
|
| 38 |
+
message_id = thread_message.id
|
| 39 |
+
print(thread_message)
|
| 40 |
+
|
| 41 |
+
print("### Step 5: Retrieve the Message ###")
|
| 42 |
+
message = client.beta.threads.messages.retrieve(
|
| 43 |
+
message_id=message_id,
|
| 44 |
+
thread_id=thread_id,
|
| 45 |
+
)
|
| 46 |
+
print(message)
|
| 47 |
+
|
| 48 |
+
print("### Step 6: Run the Assistant ###")
|
| 49 |
+
run = client.beta.threads.runs.create(
|
| 50 |
+
thread_id=thread.id,
|
| 51 |
+
assistant_id=assistant.id,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
print("### Step 7: Wait for the Assistant to Finish ###")
|
| 55 |
+
def wait_on_run(run, thread):
|
| 56 |
+
while run.status == "queued" or run.status == "in_progress":
|
| 57 |
+
run = client.beta.threads.runs.retrieve(
|
| 58 |
+
thread_id=thread.id,
|
| 59 |
+
run_id=run.id,
|
| 60 |
+
)
|
| 61 |
+
time.sleep(0.5)
|
| 62 |
+
return run
|
| 63 |
+
|
| 64 |
+
run = wait_on_run(run, thread)
|
| 65 |
+
print(run)
|
| 66 |
+
|
| 67 |
+
print("### Step 8: Retrieve the Assistant's Response ###")
|
| 68 |
+
messages = client.beta.threads.messages.list(
|
| 69 |
+
thread_id=thread.id
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
messages_str = json.dumps(messages.dict(), indent=2)
|
| 73 |
+
print(codecs.decode(messages_str, 'unicode-escape'))
|
| 74 |
+
|
| 75 |
+
answer = messages.data[0].content[0].text.value
|
| 76 |
+
print(answer)
|
| 77 |
+
return answer
|
| 78 |
+
|
| 79 |
+
# Gradio インターフェースの設定
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=assistant_response,
|
| 82 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 83 |
+
outputs="text"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# アプリケーションの起動
|
| 87 |
+
iface.launch()
|