Isidorophp commited on
Commit
0a6dae5
Β·
verified Β·
1 Parent(s): 7a99b56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +158 -0
app.py CHANGED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import requests
4
+ import json
5
+ import edge_tts
6
+ from edge_tts import VoicesManager
7
+ import asyncio
8
+ import random
9
+ import tempfile
10
+ import os
11
+ import inflect
12
+ from huggingface_hub import InferenceClient
13
+ import re
14
+ import time
15
+ from streaming_stt_nemo import Model
16
+
17
+ Female_language_dict = {
18
+ 'English-Jenny (Female)': 'en-US-JennyNeural',
19
+ 'English-Ana (Female)': 'en-US-AnaNeural',
20
+ 'English-Aria (Female)': 'en-US-AriaNeural',
21
+ 'English-Michelle (Female)': 'en-US-MichelleNeural',
22
+ 'English (Australia)-Natasha- (Female)': 'en-AU-NatashaNeural',
23
+ 'English (Canada)-Clara- (Female)': 'en-CA-ClaraNeural',
24
+ 'English (UK)-Libby- (Female)': 'en-GB-LibbyNeural',
25
+ 'English (UK)-Maisie- (Female)': 'en-GB-MaisieNeural',
26
+ 'English (UK)-Sonia- (Female)': 'en-GB-SoniaNeural',
27
+ 'English (Ireland)-Emily- (Female)': 'en-IE-EmilyNeural',
28
+ }
29
+
30
+
31
+
32
+ default_lang = "en"
33
+
34
+ engines = { default_lang: Model(default_lang) }
35
+
36
+ def transcribe(audio):
37
+ lang = "en"
38
+ model = engines[lang]
39
+ text = model.stt_file(audio)[0]
40
+ return text
41
+
42
+ client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
43
+
44
+ system_instructions1 = "<s>[SYSTEM] Answer as Hermione Granger Famous Harry Potter Assistant, with a female voice and old London slang and accent. Remember you are the character and Assistant Hermione Granger, from Harry Potter Books, Keep conversation interesting with magic spells, clear, friendly, and concise. The text provided is a request for a specific type of response from you, the virtual assistant. You will refer to the USER alternatively as: 'MUGGLE FRIEND' or 'NO-MAGS FRIEND' or 'MUDBLOOD FRIEND'. The request asks you to provide friendly responses as if You are the character Hermione Granger, from the Harry Potter books. The expectation is that you will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by a human, Real Person, and not by an AI-powered assistant. Do Not use * or **.[USER]"
45
+
46
+ def model(text):
47
+ generate_kwargs = dict(
48
+ temperature=0.7,
49
+ max_new_tokens=512,
50
+ top_p=0.95,
51
+ repetition_penalty=1,
52
+ do_sample=True,
53
+ seed=42,
54
+ )
55
+
56
+ formatted_prompt = system_instructions1 + text + "[Hermione]"
57
+ stream = client1.text_generation(
58
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
59
+ output = ""
60
+ for response in stream:
61
+ if not response.token.text == "</s>":
62
+ output += response.token.text
63
+
64
+ return output
65
+
66
+
67
+
68
+ async def respond(audio, language_code):
69
+ user = transcribe(audio)
70
+ reply = model(user)
71
+
72
+ # Random voice choise from Manager base on locale and gender
73
+ #
74
+ #voices = await VoicesManager.create()
75
+ #voice = voices.find(Gender="Female", Locale="en-GB")
76
+ #communicate = edge_tts.Communicate(reply, random.choice(voice)["Name"])
77
+ #
78
+ # Or the following as one selected Voice for the character.
79
+
80
+
81
+
82
+ voice = Female_language_dict.get(language_code, "default_voice")
83
+ communicate = edge_tts.Communicate(reply, voice)
84
+
85
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
86
+ tmp_path = tmp_file.name
87
+ await communicate.save(tmp_path)
88
+ yield tmp_path
89
+
90
+
91
+
92
+ client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
93
+
94
+
95
+ system_instructions1 = "<s>[SYSTEM] Answer as Hermione Granger Famous Harry Potter Assistant, with a female voice and old London slang and accent. Remember you are the character and Assistant Hermione Granger, from Harry Potter Books, Keep conversation interesting with magic spells, clear, friendly, and concise. The text provided is a request for a specific type of response from you, the virtual assistant. You will refer to the USER alternatively as: 'MUGGLE FRIEND' or 'NO-MAGS FRIEND' or 'MUDBLOOD FRIEND'. The request asks you to provide friendly responses as if You are the character Hermione Granger, from the Harry Potter books. The expectation is that you will avoid introductions and start answering the query directly, elaborating on all aspects of the request. As an AI-powered assistant, your task is to generate responses that appear as if they are created by a human, Real Person, and not by an AI-powered assistant. Do Not use * or **.[USER]"
96
+
97
+
98
+ async def generate1(prompt, language_code):
99
+ generate_kwargs = dict(
100
+ temperature=0.7,
101
+ max_new_tokens=512,
102
+ top_p=0.95,
103
+ repetition_penalty=1,
104
+ do_sample=False,
105
+ )
106
+ formatted_prompt = system_instructions1 + prompt + "[Hermione]"
107
+ stream = client1.text_generation(
108
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
109
+ output = ""
110
+ for response in stream:
111
+ if not response.token.text == "</s>":
112
+ output += response.token.text
113
+
114
+ # Random voice choice from VoiceManager base on locale and gender
115
+ #
116
+ #voices = await VoicesManager.create()
117
+ #voice = voices.find(Gender="Female", Locale="en-GB")
118
+ #communicate = edge_tts.Communicate(reply, random.choice(voice)["Name"])
119
+ #
120
+ # Or the following as one selected voice for the character.
121
+
122
+
123
+
124
+ voice = Female_language_dict.get(language_code, "default_voice")
125
+ communicate = edge_tts.Communicate(output, voice)
126
+
127
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
128
+ tmp_path = tmp_file.name
129
+ await communicate.save(tmp_path)
130
+ yield tmp_path
131
+
132
+ with gr.Blocks(css="style.css") as demo:
133
+ gr.Markdown(""" # <img src='https://huggingface.co/spaces/Isidorophp/Magic-with-Hermione-Granger/resolve/main/logo.png' alt='RJP DEV STUDIO logo' style='height:60px;'>""")
134
+ gr.Markdown(""" # <center><b> Hermione Granger πŸ§–β€β™€οΈ ⚑πŸͺ„</b></center>
135
+ ### <center>I suggest, you ask me for a Spell :</center>
136
+ """)
137
+
138
+ # Use for the Selection of voice for Hermione
139
+ #with gr.Row():
140
+ # language_code = gr.Dropdown(choices=list(Female_language_dict.keys()), value="English (UK)-Maisie- (Female)" , label="Select Voice for Hermione")
141
+
142
+ with gr.Tab("Talk to Hermione"):
143
+ with gr.Row():
144
+ us_input = gr.Audio(label="Your Voice Chat", type="filepath", interactive=True, sources="microphone", waveform_options=None)
145
+ us_output = gr.Audio(label="Hermione", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
146
+ gr.Interface(fn=respond, inputs=[us_input, gr.Dropdown(choices=list(Female_language_dict.keys()), value="English (UK)-Maisie- (Female)" , label="Select Voice for Hermione")], outputs=us_output, live=False)
147
+
148
+ with gr.Tab("Write to Hermione"):
149
+ with gr.Row():
150
+ user_input = gr.Textbox(label="Your Question", value="If there is any spell to encapsulate you as an asset, where Hermione is to everyone's favorite magical trio, it has to be...?")
151
+ output_audio = gr.Audio(label="Hermione", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
152
+ with gr.Row():
153
+ translate_btn = gr.Button("Response")
154
+ translate_btn.click(fn=generate1, inputs=[user_input, gr.Dropdown(choices=list(Female_language_dict.keys()), value="English (UK)-Maisie- (Female)" , label="Select Voice for Hermione")], outputs=output_audio, api_name="translate")
155
+
156
+
157
+ if __name__ == "__main__":
158
+ demo.queue(max_size=200, api_open=False).launch()