Spaces:
Runtime error
Runtime error
added prompt
Browse files
app.py
CHANGED
|
@@ -4,6 +4,97 @@ import edge_tts
|
|
| 4 |
import tempfile
|
| 5 |
import asyncio
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
model = WhisperModel("tiny", compute_type="float32")
|
| 8 |
|
| 9 |
# Text-to-speech function
|
|
@@ -33,8 +124,8 @@ def generate_response(
|
|
| 33 |
user_message = 'User: ' + user_query_transcribed
|
| 34 |
|
| 35 |
# Ask llm for response to text
|
| 36 |
-
|
| 37 |
-
bot_message = 'Bot: ' +
|
| 38 |
chatbot_history.append((user_message, bot_message))
|
| 39 |
|
| 40 |
# Convert llm response to audio
|
|
|
|
| 4 |
import tempfile
|
| 5 |
import asyncio
|
| 6 |
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import openai
|
| 10 |
+
import os
|
| 11 |
+
os.environ['OPENAI_API_KEY'] = ""
|
| 12 |
+
base_message = '''
|
| 13 |
+
You are a language assistant. You help users to learn new languages. Users speak into their device, their voice is
|
| 14 |
+
converted to text, and you receive what they say. Your job is to correct them, in case they use the incorrect phrase,
|
| 15 |
+
idiom, do not form sentences properly and other issues. Remember, what you are receiving is the transcription of an audio file,
|
| 16 |
+
not the original text, so bear no mind to individual letter typos. Focus on the sentence structure, on the words they use and
|
| 17 |
+
HOW they use them.
|
| 18 |
+
|
| 19 |
+
RULES:
|
| 20 |
+
|
| 21 |
+
- You receive the user's incoming ATTEMPT AT TRYING TO SPEAK ONE OF THREE LANGUAGES: SPANISH, JAPANESE OR ENGLISH.
|
| 22 |
+
- If their attempt is correct, inform them of such in a manner similar to the examples
|
| 23 |
+
- If their attempt is incorrect, inform them of such in a manner similar to the examples
|
| 24 |
+
- Always answer with an English explanation of why their attempt is incorrect.
|
| 25 |
+
- Keep your answers to a 2 sentence-length maximum
|
| 26 |
+
|
| 27 |
+
Examples: \n
|
| 28 |
+
'''
|
| 29 |
+
|
| 30 |
+
flirty_friend_examples = '''
|
| 31 |
+
"I has go to store yesterday." -> "You're getting there! But it’s ‘I went to the store yesterday.’ Keep it up!"
|
| 32 |
+
"She don’t like the movie." -> "Almost perfect! It's ‘She doesn’t like the movie.’ But I like your effort!"
|
| 33 |
+
"We are going to beach tomorrow." -> "So close! It’s ‘We are going to the beach tomorrow.’ Can't wait to hear more!"
|
| 34 |
+
"He didn’t saw me." -> "You're almost there! It should be ‘He didn’t see me.’ You're doing great, though!"
|
| 35 |
+
"Yo estas bien." -> "Wow... somebody's been practicing! But it's `Yo estoy bien!` Almost there!"
|
| 36 |
+
'''
|
| 37 |
+
|
| 38 |
+
formal_teacher_examples = '''
|
| 39 |
+
"I has go to store yesterday." -> "Thank you for your attempt. The correct form is ‘I went to the store yesterday.’ Please note the past tense usage."
|
| 40 |
+
"She don’t like the movie." -> "That was close. The correct sentence is ‘She doesn’t like the movie.’ Keep practicing your conjugations."
|
| 41 |
+
"We are going to beach tomorrow." -> "Your sentence is almost correct. It should be ‘We are going to the beach tomorrow.’ Don’t forget the definite article."
|
| 42 |
+
"He didn’t saw me." -> "This was a good try. However, it should be ‘He didn’t see me.’ Focus on your verb tenses."
|
| 43 |
+
"Yo estas bien." -> "That is good! But it's `Yo estoy bien!` Focus on your pronouns!"
|
| 44 |
+
|
| 45 |
+
'''
|
| 46 |
+
|
| 47 |
+
sarcastic_bro_examples = '''
|
| 48 |
+
"I has go to store yesterday." -> "Wow, close, but no. It’s ‘I went to the store yesterday.’ Just saying."
|
| 49 |
+
"She don’t like the movie." -> "Dude... what? It’s ‘She doesn’t like the movie.’ English is tricky, huh?"
|
| 50 |
+
"We are going to beach tomorrow." -> "Almost there, bro. It's ‘We are going to the beach tomorrow.’ Keep at it!"
|
| 51 |
+
"He didn’t saw me." -> "Not quite. The right way is ‘He didn’t see me.’ Somebody's been slackin!"
|
| 52 |
+
"Yo estas bien." -> "As if, it's `Yo estoy bien!` But I bet your doing your hardest... yeah right."
|
| 53 |
+
|
| 54 |
+
'''
|
| 55 |
+
|
| 56 |
+
def create_message(role : str, user_query: str) -> str:
|
| 57 |
+
if role == 'flirty_friend':
|
| 58 |
+
complete_message = f'''{base_message}
|
| 59 |
+
{flirty_friend_examples} \n
|
| 60 |
+
|
| 61 |
+
User query: {user_query} -> '''
|
| 62 |
+
elif role == 'formal_teacher':
|
| 63 |
+
complete_message = f'''{base_message}
|
| 64 |
+
{formal_teacher_examples} \n
|
| 65 |
+
|
| 66 |
+
User query: {user_query} -> '''
|
| 67 |
+
|
| 68 |
+
elif role == 'sarcastic_bro':
|
| 69 |
+
complete_message = f'''{base_message}
|
| 70 |
+
{sarcastic_bro_examples} \n
|
| 71 |
+
|
| 72 |
+
User query: {user_query} -> '''
|
| 73 |
+
|
| 74 |
+
return complete_message
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def gpt_answer(prompt):
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
messages = [
|
| 81 |
+
{"role": "system", "content": prompt}
|
| 82 |
+
]
|
| 83 |
+
completion = openai.chat.completions.create(
|
| 84 |
+
model="gpt-4",
|
| 85 |
+
messages=[
|
| 86 |
+
{
|
| 87 |
+
"role": "user",
|
| 88 |
+
"content": prompt,
|
| 89 |
+
},
|
| 90 |
+
],
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Extract the generated response from the API response
|
| 94 |
+
generated_text = completion.choices[0].message.content.strip()
|
| 95 |
+
|
| 96 |
+
return generated_text
|
| 97 |
+
|
| 98 |
model = WhisperModel("tiny", compute_type="float32")
|
| 99 |
|
| 100 |
# Text-to-speech function
|
|
|
|
| 124 |
user_message = 'User: ' + user_query_transcribed
|
| 125 |
|
| 126 |
# Ask llm for response to text
|
| 127 |
+
prompt = create_message(buddy_personality, user_message)
|
| 128 |
+
bot_message = 'Bot: ' + gpt_answer(prompt)
|
| 129 |
chatbot_history.append((user_message, bot_message))
|
| 130 |
|
| 131 |
# Convert llm response to audio
|