File size: 4,021 Bytes
d809d42
 
 
 
 
 
ab9cb62
 
d809d42
ab9cb62
d809d42
 
 
 
 
 
 
ab9cb62
 
 
 
d809d42
ab9cb62
 
 
 
 
 
 
 
 
d809d42
ab9cb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d809d42
ab9cb62
 
 
 
 
 
 
 
d809d42
 
ab9cb62
 
 
 
 
 
 
 
 
d809d42
ab9cb62
d809d42
ab9cb62
 
 
 
 
 
 
 
 
 
 
 
 
 
d809d42
ab9cb62
d809d42
ab9cb62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f96a97b
ab9cb62
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import gradio as gr
import time
from utils import *
import os

grammar_prompt="""
I want you to act as a grammar mistake checker and make the sentence more fluent. You take all the user input and auto correct it. Just reply to user input with correct grammar and reasons, DO NOT reply the context of the question of the user input. If the user input is grammatically correct and fluent, just ignore it. For each wrong sentence, you will show Original, Corrected, and Reason. Sample of the conversation will show below: 

Correct: today is a good day.

Original: today is a good day.
Corrected: Today is a good day. 
Reason: Capitalize the first letter of the sentence.

###

"""
def main():        
    chat_history = [
        {"role": "system", "content": os.environ.get("SECRET_PROMPT","You are a chat bot. Talk to me!")},
    ]

    def init():
        nonlocal chat_history
        chat_history = [
            {"role": "system", "content": os.environ.get("SECRET_PROMPT","You are a chat bot. Talk to me!")},
        ]
    init()
    
    def convert_chatbox(chat_history):
        return [f"{i['role']}: {i['content']}" for i in chat_history]

    with gr.Blocks() as demo:
        chatbot = gr.Chatbot()
        with gr.Row():
            msg = gr.Textbox()
            audio = gr.Audio(source="microphone", type="filepath", streaming=False)
        player = gr.Audio( type="filepath", label="Speaker",interactive=False)
        summary = gr.Button("Summary")
        summary_box = gr.Textbox(label="Summary")
        # functions
        def respond(message):
            # TODO: replace this with real GPT model
            chat_history.append({'role': 'user', 'content': message})
            result = generate_response(chat_history)
            mesg=result['choices'][0]['message']
            print("recv: ", mesg)
            
            response = mesg['content']
            chat_history.append(mesg)
            
            # write to file
            result = tts(response)
            with open("/tmp/temp.wav", "wb") as audio_file:
                audio_file.write(result.audio_data)

            print("write to temp.wav")
            
            chatbot.value.append((message,response))
            print("chat_history: ", chatbot.value)
            
            return None, "/tmp/temp.wav", chatbot.value

        msg.submit(respond, [msg], [msg, player,chatbot])
        
        
        def transcribe(audio_file):
            print("start transcribe, ", audio_file)
            
            start = time.time()
            text = recognize_from_file(audio_file)
            print("use ", time.time()-start)
            
            print("transcribe done, ", text)
            return respond(text)

        audio.change(transcribe, [audio], [audio, player, chatbot])
        
        def summary_response():
            messages = [
            ]
            sentences = []
            for user,assistant in chatbot.value:
                sentences.append("Correct: " + user)
            messages.append({'role': 'user', 'content': grammar_prompt + "\n".join(sentences)})
            
            result = generate_response(messages)
            mesg=result['choices'][0]['message']
            corrected = mesg['content']
            print("recv: ", mesg)
            
            return corrected
        
        summary.click(summary_response, None, summary_box, queue=False)


        btn = gr.Button("导出 & 重置", type="button", label="导出 & 重置")
        outputs = gr.JSON()

        def export():
            stats_history = {}
            for i,item in enumerate(chatbot.value):
                user,assistant = item
                stats_history[str(i)] = {
                    "user": user,
                    "assistant": assistant
                }
            init()
            chatbot.value = []
            return  [], stats_history

        btn.click(export, None, [ chatbot, outputs])

    demo.launch(show_error=True)
    
if __name__ == "__main__":
    main()