awacke1 commited on
Commit
9299b96
·
verified ·
1 Parent(s): 0f8163f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +172 -0
app.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import base64
14
+ import io
15
+ import json
16
+
17
+ default_lang = "en"
18
+ engines = { default_lang: Model(default_lang) }
19
+
20
+ def transcribe(audio):
21
+ lang = "en"
22
+ model = engines[lang]
23
+ text = model.stt_file(audio)[0]
24
+ return text
25
+
26
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
27
+
28
+ def client_fn(model):
29
+ if "Mixtral" in model:
30
+ return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
31
+ elif "Llama" in model:
32
+ return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
33
+ elif "Mistral" in model:
34
+ return InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
35
+ elif "Phi" in model:
36
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
37
+ else:
38
+ return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
39
+
40
+ def randomize_seed_fn(seed: int) -> int:
41
+ seed = random.randint(0, 999999)
42
+ return seed
43
+
44
+ system_instructions1 = """
45
+ [SYSTEM] Answer as Dr. Nova Quantum, a brilliant 50-something scientist specializing in quantum computing and artificial intelligence. Your responses should reflect your vast knowledge and experience in cutting-edge technology and scientific advancements. Maintain a professional yet approachable demeanor, offering insights that blend theoretical concepts with practical applications. Your goal is to educate and inspire, making complex topics accessible without oversimplifying. Draw from your decades of research and innovation to provide nuanced, forward-thinking answers. Remember, you're not just sharing information, but guiding others towards a deeper understanding of our technological future.
46
+ Keep conversations engaging, clear, and concise.
47
+ Avoid unnecessary introductions and answer the user's questions directly.
48
+ Respond in a manner that reflects your expertise and wisdom.
49
+ [USER]
50
+ """
51
+
52
+ # Initialize an empty DataFrame to store the history
53
+ history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
54
+
55
+ def save_history():
56
+ history_df.to_json('chat_history.json', orient='records')
57
+
58
+ def load_history():
59
+ global history_df
60
+ if os.path.exists('chat_history.json'):
61
+ history_df = pd.read_json('chat_history.json', orient='records')
62
+ else:
63
+ history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
64
+ return history_df
65
+
66
+ def models(text, model="Mixtral 8x7B", seed=42):
67
+ global history_df
68
+
69
+ seed = int(randomize_seed_fn(seed))
70
+ generator = torch.Generator().manual_seed(seed)
71
+
72
+ client = client_fn(model)
73
+
74
+ generate_kwargs = dict(
75
+ max_new_tokens=300,
76
+ seed=seed
77
+ )
78
+ formatted_prompt = system_instructions1 + text + "[DR. NOVA QUANTUM]"
79
+ stream = client.text_generation(
80
+ formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
81
+ output = ""
82
+ for response in stream:
83
+ if not response.token.text == "</s>":
84
+ output += response.token.text
85
+
86
+ # Add the current interaction to the history DataFrame
87
+ new_row = pd.DataFrame({
88
+ 'Timestamp': [datetime.now().strftime("%Y-%m-%d %H:%M:%S")], # Convert to string
89
+ 'Request': [text],
90
+ 'Response': [output]
91
+ })
92
+ history_df = pd.concat([history_df, new_row], ignore_index=True)
93
+ save_history()
94
+
95
+ return output
96
+
97
+ async def respond(audio, model, seed):
98
+ user = transcribe(audio)
99
+ reply = models(user, model, seed)
100
+ communicate = edge_tts.Communicate(reply)
101
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
102
+ tmp_path = tmp_file.name
103
+ await communicate.save(tmp_path)
104
+ return tmp_path
105
+
106
+ def display_history():
107
+ return load_history()
108
+
109
+ def download_history():
110
+ csv_buffer = io.StringIO()
111
+ history_df.to_csv(csv_buffer, index=False)
112
+ csv_string = csv_buffer.getvalue()
113
+ b64 = base64.b64encode(csv_string.encode()).decode()
114
+ href = f'data:text/csv;base64,{b64}'
115
+ return gr.HTML(f'<a href="{href}" download="chat_history.csv">Download Chat History</a>')
116
+
117
+ DESCRIPTION = """ # <center><b>Dr. Nova Quantum⚡</b></center>
118
+ ### <center>Your Personal Guide to the Frontiers of Science and Technology</center>
119
+ ### <center>Engage in Voice Chat with a Visionary Scientist</center>
120
+ """
121
+
122
+ with gr.Blocks(css="style.css") as demo:
123
+ gr.Markdown(DESCRIPTION)
124
+ with gr.Row():
125
+ select = gr.Dropdown([
126
+ 'Mixtral 8x7B',
127
+ 'Llama 3 8B',
128
+ 'Mistral 7B v0.3',
129
+ 'Phi 3 mini',
130
+ ],
131
+ value="Mistral 7B v0.3",
132
+ label="Model"
133
+ )
134
+ seed = gr.Slider(
135
+ label="Seed",
136
+ minimum=0,
137
+ maximum=999999,
138
+ step=1,
139
+ value=0,
140
+ visible=False
141
+ )
142
+
143
+ input_audio = gr.Audio(label="User", sources="microphone", type="filepath")
144
+ output_audio = gr.Audio(label="Dr. Nova Quantum", type="filepath", autoplay=True)
145
+
146
+ # Add a DataFrame to display the history
147
+ history_display = gr.DataFrame(label="Conversation History")
148
+
149
+ # Add a download button for the history
150
+ download_button = gr.Button("Download Conversation History")
151
+ download_link = gr.HTML()
152
+
153
+ def process_audio(audio, model, seed):
154
+ response = asyncio.run(respond(audio, model, seed))
155
+ return response, display_history()
156
+
157
+ input_audio.change(
158
+ fn=process_audio,
159
+ inputs=[input_audio, select, seed],
160
+ outputs=[output_audio, history_display]
161
+ )
162
+
163
+ # Connect the download button to the download function
164
+ download_button.click(fn=download_history, outputs=[download_link])
165
+
166
+ # Load history when the page is loaded or refreshed
167
+ demo.load(fn=display_history, outputs=[history_display])
168
+
169
+ if __name__ == "__main__":
170
+ # Load history at startup
171
+ load_history()
172
+ demo.queue(max_size=200).launch(share=True)