File size: 1,858 Bytes
4200710
9b6e058
4e30cee
 
 
 
9b6e058
b1ebbbc
9b6e058
4e30cee
 
9b6e058
4e30cee
 
b1ebbbc
4200710
 
0369ce7
4e30cee
 
 
 
 
 
 
 
 
9b6e058
4e30cee
 
0369ce7
 
 
4e30cee
0369ce7
 
 
 
4200710
0369ce7
 
4e30cee
4200710
4e30cee
 
 
 
 
 
 
 
 
4200710
 
0369ce7
4e30cee
0369ce7
 
4e30cee
 
 
 
4200710
4e30cee
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
53
54
55
56
57
58
59
60
61
62
63
64
65
import gradio as gr
import openai
import openai_secret_manager

# Function to get the secret values
def get_secrets():
    secrets = openai_secret_manager.get_secret("INSTRUCTOR_API_KEY")
    instructor_prompt = openai_secret_manager.get_secret("INSTRUCTOR_PROMPT")
    return secrets, instructor_prompt

# Get the secrets
secrets, instructor_prompt = get_secrets()

# Set your API key
api_key = secrets["INSTRUCTOR_API_KEY"]

with gr.Blocks() as demo:
    chat_history = gr.State(value=[])

    def chat_with_instructor(message, history):
        import openai

        openai.api_key = api_key
        chat_input = instructor_prompt + "\nUser: " + message + "\nInstructor:"
        response = openai.Completion.create(
            engine="text-davinci-002",
            prompt=chat_input,
            max_tokens=1024  # Adjust this for response length
        ).choices[0].text.strip()

        chat_history.value.append(
            {
                "user": message,
                "instructor": instructor_prompt,
                "bot": response,
            }
        )
        return response

    def generate_json(chat_history):
        return chat_history.value

    chatbox = gr.ChatInterface(
        fn=chat_with_instructor,

        title="Chat with Instructor",
        description="Chat with the instructor using a predefined prompt.",
        examples=["Hi, can you help me with this?"],

        submit_btn="Send",
        stop_btn="Stop",
        retry_btn="Retry",
        undo_btn="Undo last message",
        clear_btn="Start a new conversation"
    ).queue()

    chat_history_json = gr.JSON(generate_json(chat_history))
    gr.Markdown("### 📩 Generate the JSON file for your chat history!")
    gr.Interface(fn=generate_json,
                 inputs=None,
                 outputs=[chat_history_json])

demo.queue()
demo.launch()