File size: 1,609 Bytes
3293a8a
819c471
d41b583
4e30cee
eb7e4f2
d41b583
 
4200710
 
0369ce7
4e30cee
 
 
 
 
 
 
 
4834c3d
4e30cee
 
0369ce7
 
 
4e30cee
0369ce7
 
 
 
4200710
0369ce7
 
4e30cee
14306c2
4e30cee
 
 
 
 
 
 
 
 
4200710
 
0369ce7
4e30cee
0369ce7
 
4e30cee
 
 
 
14306c2
59e7856
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
import gradio as gr
import openai
import os

# Retrieve the API key from the Hugging Face secret
api_key = os.environ["INSTRUCTOR_API_KEY"]
instructor_prompt = os.environ["INSTRUCTOR_PROMPT"]

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

    def chat_with_instructor(message, history):

        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=50  # 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

    demo = 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(share=True)