Chan55 commited on
Commit
67929ed
Β·
1 Parent(s): 6bfde08

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -0
app.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #
2
+
3
+
4
+ # openai.api_key = "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"
5
+ # set_api_key("5d879e23b3f825be89038d704485ea5a")
6
+ import openai
7
+
8
+ from elevenlabs import set_api_key
9
+
10
+ set_api_key("24efc219fc704fe895cee524fc66d529")
11
+
12
+ openai.api_key = "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"
13
+
14
+ import whisper
15
+
16
+ model = whisper.load_model("base")
17
+ import os
18
+ os.environ['PATH'] += os.pathsep + '/path/to/mpv/'
19
+ from typing import Optional
20
+
21
+
22
+ class Chat:
23
+
24
+ def __init__(self , system: Optional[str] = None):
25
+ self.system = system
26
+ self.messages = []
27
+
28
+ if system is not None:
29
+ self.messages.append({
30
+ "role": "system",
31
+ "content": system
32
+ })
33
+
34
+ def prompt(self, content: str) -> str:
35
+ self.messages.append({
36
+ "role": "user",
37
+ "content": content
38
+ })
39
+ response = openai.ChatCompletion.create(
40
+ model="gpt-3.5-turbo",
41
+ messages=self.messages
42
+ )
43
+ response_content = response["choices"][0]["message"]["content"]
44
+ self.messages.append({
45
+ "role": "assistant",
46
+ "content": response_content
47
+ })
48
+ return response_content
49
+
50
+
51
+ from elevenlabs import generate, play , stream
52
+
53
+ import gradio as gr
54
+
55
+ def read_and_print_file(file_path):
56
+ with open(file_path, 'r') as file:
57
+ return file.read()
58
+
59
+
60
+ # Example usage:
61
+ file_path = 'the_interview_questions'
62
+ contents = read_and_print_file(file_path)
63
+
64
+
65
+ chat = Chat(system="""You have to take the annual financial review. To ensure the user have the most up-to-date information, you have to ask a few questions.
66
+ Make Sure the following information is being extracted after the Interview:(Ask the questions by one by one not in a big chunk, ask the question and wait for his response and then ask the second questions)
67
+ Ask question in your own way that we can know that (ask the questions one by one slowly don't rush and wait for user repsponse for first question and then ask the second question)
68
+ remember to ask the below questions one after one like a interview not all at a time.
69
+ wait for user response after one single questions until then don't ask next question
70
+ remember to not to include question number.
71
+ """ + contents + """
72
+ """)
73
+
74
+
75
+
76
+
77
+ def convert_tuples_to_list(tuples_list):
78
+ result = []
79
+ conversation = ""
80
+ for tuple_item in tuples_list:
81
+ result.append(tuple_item[0]) # Append question
82
+ result.append(tuple_item[1]) # Append answer
83
+ for i in result:
84
+ conversation = conversation + i + " \n"
85
+ return conversation
86
+
87
+
88
+ def run_text_prompt(message, chat_history):
89
+ bot_message = chat.prompt(content=message)
90
+
91
+ audio = generate(
92
+ text=bot_message,
93
+ voice="Antoni",
94
+ )
95
+
96
+ play(audio)
97
+
98
+ chat_history.append((message, bot_message))
99
+ return "", chat_history
100
+
101
+
102
+ def run_audio_prompt(audio, chat_history):
103
+ if audio is None:
104
+ return None, chat_history
105
+
106
+ message_transcription = model.transcribe(audio)["text"]
107
+ _, chat_history = run_text_prompt(message_transcription, chat_history)
108
+ return None, chat_history
109
+ file_path = "myfile.txt"
110
+
111
+ try:
112
+ with open(file_path, "r") as file:
113
+ file_contents = file.read()
114
+
115
+ except FileNotFoundError:
116
+ print(f"File not found at: {file_path}")
117
+ except IOError:
118
+ print(f"Error reading file: {file_path}")
119
+
120
+ def process_text(conversation, text):
121
+ # print(text)
122
+ completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",
123
+ messages=[{
124
+ "role":
125
+ "user",
126
+ "content":
127
+ conversation + text
128
+ }])
129
+ # completion = text;
130
+ # print(completion)
131
+ # print("Your 710 words has been translated. ")
132
+ print(completion.choices[0].message.content.strip())
133
+ return completion.choices[0].message.content.strip()
134
+
135
+ def inference(text):
136
+ html = (
137
+ "<div >"
138
+ "<img src='C://Users//l//Desktop//pythonProject33//thumbnail_IAI_icon.png' alt='image One'>"
139
+ + "</div>"
140
+ )
141
+ return html
142
+ with gr.Blocks(title="hi") as app2:
143
+ gr.HTML("""<img src="C://Users//l//Desktop//pythonProject33//thumbnail_IAI_icon.png" alt="A beautiful landscape">""")
144
+ gr.Markdown("""<h1><center>Interview Phase 1</center></h1>
145
+ <img src="thumbnail_IAI_icon.png" >""")
146
+ chatbot = gr.Chatbot()
147
+
148
+ msg = gr.Textbox()
149
+ msg.submit(run_text_prompt, [msg, chatbot], [msg, chatbot])
150
+
151
+ with gr.Row():
152
+ audio = gr.Audio(source="microphone", type="filepath")
153
+
154
+ fn = run_audio_prompt,
155
+ inputs = [audio, chatbot],
156
+ outputs = [audio, chatbot]
157
+
158
+
159
+ # fn = run_audio_prompt,
160
+ # inputs = [audio, chatbot],
161
+ # outputs = [audio, chatbot]
162
+
163
+ send_audio_button = gr.Button("Send Audio", interactive=True)
164
+ send_audio_button.click(run_audio_prompt, [audio, chatbot], [audio, chatbot])
165
+
166
+ demo = gr.TabbedInterface([app2], [ "Interview"])
167
+
168
+ demo.launch(share=False,
169
+ debug=False,
170
+ server_name="localhost")
171
+