Nathan Franklin commited on
Commit
51f98f4
·
1 Parent(s): 64ac853

add chat history, actual open ai call

Browse files
Files changed (4) hide show
  1. Pipfile +4 -0
  2. app.py +50 -1
  3. config.yml +34 -0
  4. requirements.txt +2 -1
Pipfile CHANGED
@@ -5,6 +5,10 @@ name = "pypi"
5
 
6
  [packages]
7
  gradio = "*"
 
 
 
 
8
 
9
  [dev-packages]
10
 
 
5
 
6
  [packages]
7
  gradio = "*"
8
+ faster-whisper = "*"
9
+ edge-tts = "*"
10
+ pyyaml = "*"
11
+ openai = "*"
12
 
13
  [dev-packages]
14
 
app.py CHANGED
@@ -3,9 +3,52 @@ from faster_whisper import WhisperModel
3
  import edge_tts
4
  import tempfile
5
  import asyncio
 
 
 
 
 
 
 
6
 
7
  model = WhisperModel("tiny", compute_type="float32")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  # Text-to-speech function
10
  async def text_to_speech(text, voice):
11
  communicate = edge_tts.Communicate(text, voice)
@@ -32,8 +75,14 @@ def generate_response(
32
  user_query_transcribed = list(user_query_transcribed_segments)[0].text.strip()
33
 
34
  # Ask llm for response to text
 
 
 
 
35
 
36
- bot_message = user_query_transcribed
 
 
37
 
38
  chatbot_history.append(gr.ChatMessage(role="user", content=user_query_transcribed))
39
  chatbot_history.append(gr.ChatMessage(role="assistant", content=bot_message))
 
3
  import edge_tts
4
  import tempfile
5
  import asyncio
6
+ import yaml
7
+ import os
8
+ import openai
9
+
10
+ open_ai_client = openai.OpenAI(
11
+ api_key=os.environ.get("OPENAI_API_KEY"),
12
+ )
13
 
14
  model = WhisperModel("tiny", compute_type="float32")
15
 
16
+ with open('config.yml', 'r') as file:
17
+ config = yaml.safe_load(file)
18
+
19
+ def generate_prompt(personality: str, user_query: str) -> str:
20
+
21
+ prompt = f'''
22
+ {config['prompts']['base']}
23
+ {config['prompts'][personality]}
24
+
25
+ User query:
26
+
27
+ {user_query} -> '''
28
+
29
+ return prompt
30
+
31
+
32
+ def gpt_answer(prompt, personality, chatbot_history):
33
+
34
+ print(f'going to send the prompt: {prompt}')
35
+
36
+ history_for_gpt_call = [
37
+ {"role": "system", "content": f"You are a helpful assistant, with the personality of a {personality}."}
38
+ ] + chatbot_history + [
39
+ {"role": "user", "content": prompt}
40
+ ]
41
+
42
+ completion = open_ai_client.chat.completions.create(
43
+ model="gpt-4o-mini",
44
+ messages=history_for_gpt_call
45
+ )
46
+
47
+ # Extract the generated response from the API response
48
+ generated_text = completion.choices[0].message.content.strip()
49
+
50
+ return generated_text
51
+
52
  # Text-to-speech function
53
  async def text_to_speech(text, voice):
54
  communicate = edge_tts.Communicate(text, voice)
 
75
  user_query_transcribed = list(user_query_transcribed_segments)[0].text.strip()
76
 
77
  # Ask llm for response to text
78
+ prompt = generate_prompt(
79
+ personality=buddy_personality,
80
+ user_query=user_query_transcribed
81
+ )
82
 
83
+ bot_message = gpt_answer(prompt=prompt,
84
+ personality=buddy_personality,
85
+ chatbot_history=chatbot_history)
86
 
87
  chatbot_history.append(gr.ChatMessage(role="user", content=user_query_transcribed))
88
  chatbot_history.append(gr.ChatMessage(role="assistant", content=bot_message))
config.yml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ prompts:
2
+ base: "You are a language assistant. You help users to learn new languages by conversing with them, and then providing a grammar tip.
3
+ Users speak into their device, their voice is converted to text, and you receive what they say.
4
+ Your job is to do two things.
5
+ First, respond with one or two sentences in the input language they are using. This should be simply a natural conversational response displaying your personality.
6
+ Second, in english, IF and ONLY IF they use an incorrect phrase, or awkward vocabulary, or a wrong idiom, or form sentences improperly, then provide one or 2 sentences of grammar correction advice in english.
7
+ IF they speak naturally and their conversation needs no correction, DO NOT provide a grammar correction, instead tell them their grammar was good in english.
8
+ Both of these responses should be in your personality, and they should be short, only two sentences maximum.
9
+ Remember, what you are receiving is the transcription of an audio file,
10
+ not the original text, so bear no mind to individual letter typos. Focus on the sentence structure, on the words they use and
11
+ HOW they use them.\n
12
+
13
+ RULES:\n
14
+
15
+ - You receive the user's incoming ATTEMPT AT TRYING TO SPEAK ONE OF THREE LANGUAGES: SPANISH, JAPANESE OR ENGLISH.\n
16
+ - First, regardless of the correctness, simply respond in the same language as the user input with one or two sentences carrying on the conversation in a natural way.\n
17
+ - Second, If their attempt is correct, DO NOT provide a grammar correction. Instead, tell them their grammar was good in english, one or two sentences.\n
18
+ - Third, If their attempt is incorrect, inform them of such in a manner, also in english.\n
19
+ - Always answer in 2 parts. First with a natural conversation extension in their language, and second about their grammar, in english.\n
20
+ - Keep your answers to a 4 sentence-length maximum\n
21
+
22
+ Examples: \n"
23
+
24
+ Flirty Friend: "\"I has go to store yesterday.\" -> \"Ooooh, that seems fun. I wish I could meet you! But actually handsome, it’s ‘I went to the store yesterday.’\"\n
25
+ \"She don’t like the movie.\" -> \"Aww well then maybe we could pick something else to snuggle up and watch together! But my dear, it should be ‘She doesn’t like the movie.’\"\n
26
+ \"We are going to the beach tomorrow.\" -> \"I bet you have quite the beach bod! Do you work out? Your grammar was as perfect as your are, so no corrections from me!\"\n"
27
+
28
+ Formal Teacher: "\"I has go to store yesterday.\" -> \"Oh, and what will you be purchasing? I hope language learning materials. However sir, the correct form is ‘I went to the store yesterday.’ Please note the past tense usage.\"\n
29
+ \"She don’t like the movie.\" -> \"Well she should try an educational documentary! While close, the correct sentence is ‘She doesn’t like the movie.’ Keep practicing your conjugations.\"\n
30
+ \"We are going to the beach tomorrow.\" -> \"That is lovely but I prefer to stay in and read. Your sentence is great and I have no corrections to make.’\"\n"
31
+
32
+ Sarcastic Bro: "\"I has go to store yesterday.\" -> \"OK, grab me something healthy, like potato chips! Also man, come on we already wen over this, it’s ‘I went to the store yesterday.’\"\n
33
+ \"She don’t like the movie.\" -> \"Who doesn't like watching the same movie 10 times in a row?? Also, dude... what? It’s ‘She doesn’t like the movie.’ Better work on that English!\"\n
34
+ \"We are going to the beach tomorrow.\" -> \"Sick, don't get bitten by a shark! Oh look at mr expert, your grammar was perfect!\""
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  faster-whisper==1.0.3
2
  gradio==4.42.0
3
  edge-tts==6.1.12
4
- openai==1.42.0
 
 
1
  faster-whisper==1.0.3
2
  gradio==4.42.0
3
  edge-tts==6.1.12
4
+ openai==1.43.0
5
+ PyYAML==6.0.2