awacke1 commited on
Commit
4c2a30d
·
verified ·
1 Parent(s): 328c25a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
+ import tempfile
5
+ import os
6
+ from huggingface_hub import InferenceClient
7
+ import re
8
+ from streaming_stt_nemo import Model
9
+ import torch
10
+ import random
11
+ import pandas as pd
12
+ from datetime import datetime
13
+
14
+ default_lang = "en"
15
+ engines = { default_lang: Model(default_lang) }
16
+
17
+ def transcribe(audio):
18
+ lang = "en"
19
+ model = engines[lang]
20
+ text = model.stt_file(audio)[0]
21
+ return text
22
+
23
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
24
+
25
+ def client_fn(model):
26
+ if "Mixtral" in model:
27
+ return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
28
+ elif "Llama" in model:
29
+ return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
30
+ elif "Mistral" in model:
31
+ return InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
32
+ elif "Phi" in model:
33
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
34
+ else:
35
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
36
+
37
+ def randomize_seed_fn(seed: int) -> int:
38
+ seed = random.randint(0, 999999)
39
+ return seed
40
+
41
+ system_instructions1 = """
42
+ [SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark.'
43
+ Keep conversation friendly, short, clear, and concise.
44
+ Avoid unnecessary introductions and answer the user's questions directly.
45
+ Respond in a normal, conversational manner while being friendly and helpful.
46
+ [USER]
47
+ """
48
+
49
+ # Initialize an empty DataFrame to store the history
50
+ history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
51
+
52
+ def models(text, model="Mixtral 8x7B", seed=42):
53
+ global history_df
54
+
55
+ seed = int(randomize_seed_fn(seed))
56
+ generator = torch.Generator().manual_seed(seed)
57
+
58
+ client = client_fn(model)
59
+
60
+ generate_kwargs = dict(
61
+ max_new_tokens=300,
62
+ seed=seed
63
+ )
64
+ formatted_prompt = system_instructions1 + text + "[JARVIS]"
65
+ stream = client.text_generation(
66
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
67
+ output = ""
68
+ for response in stream:
69
+ if not response.token.text == "</s>":
70
+ output += response.token.text
71
+
72
+ # Add the current interaction to the history DataFrame
73
+ new_row = pd.DataFrame({
74
+ 'Timestamp': [datetime.now()],
75
+ 'Request': [text],
76
+ 'Response': [output]
77
+ })
78
+ history_df = pd.concat([history_df, new_row], ignore_index=True)
79
+
80
+ return output
81
+
82
+ async def respond(audio, model, seed):
83
+ user = transcribe(audio)
84
+ reply = models(user, model, seed)
85
+ communicate = edge_tts.Communicate(reply)
86
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
87
+ tmp_path = tmp_file.name
88
+ await communicate.save(tmp_path)
89
+ yield tmp_path
90
+
91
+ def display_history():
92
+ return history_df
93
+
94
+ def download_history():
95
+ return history_df.to_csv(index=False)
96
+
97
+ DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
98
+ ### <center>A personal Assistant of Tony Stark for YOU
99
+ ### <center>Voice Chat with your personal Assistant</center>
100
+ """
101
+
102
+ with gr.Blocks(css="style.css") as demo:
103
+ gr.Markdown(DESCRIPTION)
104
+ with gr.Row():
105
+ select = gr.Dropdown([ 'Mixtral 8x7B',
106
+ 'Llama 3 8B',
107
+ 'Mistral 7B v0.3',
108
+ 'Phi 3 mini',
109
+ ],
110
+ value="Mistral 7B v0.3",
111
+ label="Model"
112
+ )
113
+ seed = gr.Slider(
114
+ label="Seed",
115
+ minimum=0,
116
+ maximum=999999,
117
+ step=1,
118
+ value=0,
119
+ visible=False
120
+ )
121
+ input = gr.Audio(label="User", sources="microphone", type="filepath", waveform_options=False)
122
+ output = gr.Audio(label="AI", type="filepath",
123
+ interactive=False,
124
+ autoplay=True,
125
+ elem_classes="audio")
126
+
127
+ # Add a DataFrame to display the history
128
+ history_display = gr.DataFrame(label="Query History")
129
+
130
+ # Add a download button for the history
131
+ download_button = gr.Button("Download History")
132
+
133
+ gr.Interface(
134
+ batch=True,
135
+ max_batch_size=10,
136
+ fn=respond,
137
+ inputs=[input, select, seed],
138
+ outputs=[output], live=True)
139
+
140
+ # Update the history display after each interaction
141
+ output.change(fn=display_history, outputs=[history_display])
142
+
143
+ # Connect the download button to the download function
144
+ download_button.click(fn=download_history, outputs=[gr.File(label="Download CSV")])
145
+
146
+ if __name__ == "__main__":
147
+ demo.queue(max_size=200).launch()