File size: 2,112 Bytes
73d2dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import gradio as gr
import json
from datetime import datetime
import openai


# Assistant Creation function
def create_assistant_json(uploaded_file, assistant_name,  assistant_message):
    client = openai.OpenAI(api_key=os.environ["API_TOKEN"])
    # Check if a file was uploaded
    print(uploaded_file)
    df = open(uploaded_file, "rb")
    file = client.files.create(file=df,
                               purpose='assistants')

    assistant = client.beta.assistants.create(
        name=assistant_name,
        instructions=assistant_message,
        model="gpt-4-0125-preview",
        tools=[
            {
                "type": "retrieval"  # This adds the knowledge base as a tool
            }
        ],
        file_ids=[file.id])
    
    return assistant.id

# Creating the Gradio interface
with gr.Blocks() as demo:
    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...")
    gr.Markdown("## After creating the ID head to [OpenAI_Assistant_Chat](https://huggingface.co/spaces/jadend/OpenAI_Assistant_Chat).")
    with gr.Row():
        file_input = gr.File(label="Upload your file", type="filepath")
        assistant_name = gr.Textbox(label="The Assistant's Name")
        assistant_message = gr.Textbox(label="Assistant Message")
    generate_button = gr.Button("Generate Your Assistant ID") 
    output_id = gr.Textbox(label="Your Asssistant ID", value="")
    
    generate_button.click(
        fn=create_assistant_json,
        inputs=[file_input, assistant_name, assistant_message],
        outputs=output_id
    )

if __name__ == "__main__":
    demo.launch(#enable_queue=False,
        # Creates an auth screen 
        auth=lambda u, p: user_db.get(u) == p,
        auth_message="Welcome! Enter a Username and Password"
               ).queue()