AdamyaG commited on
Commit
5f7232d
1 Parent(s): 08d5b9f

Create audio_jarvis.py

Browse files
Files changed (1) hide show
  1. audio_jarvis.py +148 -0
audio_jarvis.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import requests
4
+ import json
5
+ import edge_tts
6
+ import asyncio
7
+ import tempfile
8
+ import os
9
+ import inflect
10
+ from huggingface_hub import InferenceClient
11
+ import re
12
+ import time
13
+ from streaming_stt_nemo import Model
14
+
15
+ number_to_word = inflect.engine()
16
+
17
+ default_lang = "en"
18
+
19
+ engines = { default_lang: Model(default_lang) }
20
+
21
+ def transcribe(audio):
22
+ lang = "en"
23
+ model = engines[lang]
24
+ text = model.stt_file(audio)[0]
25
+ return text
26
+
27
+ client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
28
+
29
+ system_instructions1 = "<s>[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'Tony Stark.' The expectation is that I 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 the user and not an AI-powered assistant. [USER]"
30
+
31
+ def model(text):
32
+ generate_kwargs = dict(
33
+ temperature=0.7,
34
+ max_new_tokens=512,
35
+ top_p=0.95,
36
+ repetition_penalty=1,
37
+ do_sample=True,
38
+ seed=42,
39
+ )
40
+
41
+ formatted_prompt = system_instructions1 + text + "[JARVIS]"
42
+ stream = client1.text_generation(
43
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
44
+ output = ""
45
+ for response in stream:
46
+ if not response.token.text == "</s>":
47
+ output += response.token.text
48
+
49
+ return output
50
+
51
+ def number_to_words(str):
52
+ words = str.split(' ')
53
+ result = []
54
+
55
+ for word in words:
56
+ if( any(char.isdigit() for char in word) ):
57
+ word = number_to_word.number_to_words(word)
58
+
59
+ result.append(word)
60
+
61
+ final_result = ' '.join(result).replace('point', '')
62
+ return final_result
63
+
64
+ async def respond(audio):
65
+ user = transcribe(audio)
66
+ reply = model(user)
67
+ reply2 = number_to_words(reply)
68
+ communicate = edge_tts.Communicate(reply2)
69
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
70
+ tmp_path = tmp_file.name
71
+ await communicate.save(tmp_path)
72
+ yield tmp_path
73
+
74
+ DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
75
+ ### <center>A personal Assistant of Tony Stark for YOU
76
+ ### <center>Voice Chat with your personal Assistant</center>
77
+ """
78
+
79
+ MORE = """ ## TRY Other Models
80
+ ### Instant Video: Create Amazing Videos in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Video
81
+ ### Instant Image: 4k images in 5 Second -> https://huggingface.co/spaces/KingNish/Instant-Image
82
+ """
83
+
84
+ BETA = """ ### Voice Chat (BETA)"""
85
+
86
+ FAST = """## Fastest Model"""
87
+
88
+ Complex = """## Best in Complex Question"""
89
+
90
+ Detail = """## Best for Detailed Generation or Long Answers"""
91
+
92
+ base_loaded = "mistralai/Mixtral-8x7B-Instruct-v0.1"
93
+
94
+ client1 = InferenceClient(base_loaded)
95
+
96
+ system_instructions1 = "[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark', Keep conversation very short, clear, friendly and concise. The text provided is a request for a specific type of response from you, the virtual assistant. The request asks you to provide friendly responses as if You are the character Jarvis, made by 'Tony Stark.' The expectation is that I 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 the user and not an AI-powered assistant. [USER]"
97
+
98
+ async def generate1(prompt):
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 + "[JARVIS]"
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
+ communicate = edge_tts.Communicate(output)
115
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
116
+ tmp_path = tmp_file.name
117
+ await communicate.save(tmp_path)
118
+ yield tmp_path
119
+
120
+ with gr.Blocks(css="style.css") as demo:
121
+ gr.Markdown(DESCRIPTION)
122
+ with gr.Row():
123
+ user_input = gr.Audio(label="Voice Chat (BETA)", type="filepath")
124
+ output_audio = gr.Audio(label="JARVIS", type="filepath",
125
+ interactive=False,
126
+ autoplay=True,
127
+ elem_classes="audio")
128
+ with gr.Row():
129
+ translate_btn = gr.Button("Response")
130
+ translate_btn.click(fn=respond, inputs=user_input,
131
+ outputs=output_audio, api_name=False)
132
+ gr.Markdown(FAST)
133
+ with gr.Row():
134
+ user_input = gr.Textbox(label="Prompt", value="What is Wikipedia")
135
+ input_text = gr.Textbox(label="Input Text", elem_id="important")
136
+ output_audio = gr.Audio(label="JARVIS", type="filepath",
137
+ interactive=False,
138
+ autoplay=True,
139
+ elem_classes="audio")
140
+ with gr.Row():
141
+ translate_btn = gr.Button("Response")
142
+ translate_btn.click(fn=generate1, inputs=user_input,
143
+ outputs=output_audio, api_name="translate")
144
+
145
+ gr.Markdown(MORE)
146
+
147
+ if __name__ == "__main__":
148
+ demo.queue(max_size=200).launch()