Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
from datetime import datetime
|
5 |
+
import openai
|
6 |
+
|
7 |
+
|
8 |
+
# Assistant Creation function
|
9 |
+
def create_assistant_json(uploaded_file, assistant_name, assistant_message):
|
10 |
+
client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
|
11 |
+
# Check if a file was uploaded
|
12 |
+
print(uploaded_file)
|
13 |
+
df = open(uploaded_file, "rb")
|
14 |
+
file = client.files.create(file=df,
|
15 |
+
purpose='assistants')
|
16 |
+
|
17 |
+
assistant = client.beta.assistants.create(
|
18 |
+
name=assistant_name,
|
19 |
+
instructions=assistant_message,
|
20 |
+
model="gpt-4-0125-preview",
|
21 |
+
tools=[
|
22 |
+
{
|
23 |
+
"type": "retrieval" # This adds the knowledge base as a tool
|
24 |
+
}
|
25 |
+
],
|
26 |
+
file_ids=[file.id])
|
27 |
+
|
28 |
+
return assistant.id
|
29 |
+
|
30 |
+
# Creating the Gradio interface
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("## To create an OpenAI Assistant please fill in the following sections. Upload a file to give the Assistant knowledge and a focus on something outside of it's normal training. Then add an assistant name and message. The Assistant message should guide the model into in a role. An example would be, You are a helpful Asssitant who is knowledgable in the field of...")
|
33 |
+
gr.Markdown("## After creating the ID head to [OpenAI_Assistant_Chat](https://huggingface.co/spaces/jadend/OpenAI_Assistant_Chat).")
|
34 |
+
with gr.Row():
|
35 |
+
file_input = gr.File(label="Upload your file", type="filepath")
|
36 |
+
assistant_name = gr.Textbox(label="The Assistant's Name")
|
37 |
+
assistant_message = gr.Textbox(label="Assistant Message")
|
38 |
+
generate_button = gr.Button("Generate Your Assistant ID")
|
39 |
+
output_id = gr.Textbox(label="Your Asssistant ID", value="")
|
40 |
+
|
41 |
+
generate_button.click(
|
42 |
+
fn=create_assistant_json,
|
43 |
+
inputs=[file_input, assistant_name, assistant_message],
|
44 |
+
outputs=output_id
|
45 |
+
)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
48 |
+
demo.launch(#enable_queue=False,
|
49 |
+
# Creates an auth screen
|
50 |
+
auth=lambda u, p: user_db.get(u) == p,
|
51 |
+
auth_message="Welcome! Enter a Username and Password"
|
52 |
+
).queue()
|