Letsch22 commited on
Commit
d167996
1 Parent(s): 4e35c68

example app

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import openai
4
+
5
+
6
+ user_db = {
7
+ os.environ["username"]: os.environ["password"],
8
+ }
9
+
10
+ # Assistant Creation function
11
+ def create_assistant_json(uploaded_file, assistant_name, assistant_message):
12
+ client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
13
+ # Check if a file was uploaded
14
+ print(uploaded_file)
15
+ df = open(uploaded_file, "rb")
16
+ file = client.files.create(file=df,
17
+ purpose='assistants')
18
+
19
+ assistant = client.beta.assistants.create(
20
+ name=assistant_name,
21
+ instructions=assistant_message,
22
+ model="gpt-4-0125-preview",
23
+ tools=[
24
+ {
25
+ "type": "retrieval" # This adds the knowledge base as a tool
26
+ }
27
+ ],
28
+ file_ids=[file.id])
29
+
30
+ return assistant.id
31
+
32
+ # Creating the Gradio interface
33
+ with gr.Blocks() as demo:
34
+ 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...")
35
+ gr.Markdown("## After creating the ID head to [OpenAI_Assistant_Chat](https://huggingface.co/spaces/jadend/OpenAI_Assistant_Chat).")
36
+ with gr.Row():
37
+ file_input = gr.File(label="Upload your file", type="filepath")
38
+ assistant_name = gr.Textbox(label="The Assistant's Name")
39
+ assistant_message = gr.Textbox(label="Assistant Message")
40
+ generate_button = gr.Button("Generate Your Assistant ID")
41
+ output_id = gr.Textbox(label="Your Asssistant ID", value="")
42
+
43
+ generate_button.click(
44
+ fn=create_assistant_json,
45
+ inputs=[file_input, assistant_name, assistant_message],
46
+ outputs=output_id
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch(#enable_queue=False,
51
+ # Creates an auth screen
52
+ auth=lambda u, p: user_db.get(u) == p,
53
+ auth_message="Welcome! Enter a Username and Password"
54
+ ).queue()