simonraj commited on
Commit
0441ef7
·
verified ·
1 Parent(s): 9ef74ff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import tempfile
3
+ import asyncio
4
+ import gradio as gr
5
+ from streaming_stt_nemo import Model
6
+ from huggingface_hub import InferenceClient
7
+ import edge_tts
8
+
9
+ default_lang = "en"
10
+ engines = {default_lang: Model(default_lang)}
11
+
12
+ def transcribe(audio):
13
+ lang = "en"
14
+ model = engines[lang]
15
+ text = model.stt_file(audio)[0]
16
+ return text
17
+
18
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
19
+
20
+ system_instructions = "[SYSTEM] Answer as Real OpenGPT 4o, Made by 'KingNish', 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. 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 the user and not an AI-powered assistant. [USER]"
21
+
22
+ def model(text):
23
+ generate_kwargs = dict(
24
+ temperature=0.7,
25
+ max_new_tokens=512,
26
+ top_p=0.95,
27
+ repetition_penalty=1,
28
+ do_sample=True,
29
+ seed=42,
30
+ )
31
+
32
+ formatted_prompt = system_instructions + text + "[OpenGPT 4o]"
33
+ stream = client.text_generation(
34
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
35
+ output = ""
36
+ for response in stream:
37
+ if not response.token.text == "</s>":
38
+ output += response.token.text
39
+
40
+ return output
41
+
42
+ @spaces.GPU(duration=120) # Increase duration if needed
43
+ async def respond(audio):
44
+ user = transcribe(audio)
45
+ reply = model(user)
46
+ communicate = edge_tts.Communicate(reply)
47
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
48
+ tmp_path = tmp_file.name
49
+ await communicate.save(tmp_path)
50
+ return tmp_path
51
+
52
+ with gr.Blocks() as voice:
53
+ with gr.Row():
54
+ input = gr.Audio(label="Voice Chat", source="microphone", type="filepath")
55
+ output = gr.Audio(label="OpenGPT 4o", type="filepath", interactive=False, autoplay=True)
56
+
57
+ gr.Interface(
58
+ fn=respond,
59
+ inputs=[input],
60
+ outputs=[output],
61
+ live=True,
62
+ )
63
+
64
+ theme = gr.themes.Base()
65
+
66
+ with gr.Blocks(theme=theme, css="footer {visibility: hidden}textbox{resize:none}", title="GPT 4o DEMO") as demo:
67
+ gr.Markdown("# OpenGPT 4o")
68
+ gr.TabbedInterface([voice], ['🗣️ Voice Chat'])
69
+
70
+ demo.queue(max_size=200)
71
+ demo.launch()