Letsch22 commited on
Commit
35accf7
·
1 Parent(s): 4c0b507

app example with OpenAI API

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