Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,85 @@ from datetime import datetime
|
|
13 |
import base64
|
14 |
import io
|
15 |
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def download_history():
|
19 |
csv_buffer = io.StringIO()
|
@@ -21,7 +99,7 @@ def download_history():
|
|
21 |
csv_string = csv_buffer.getvalue()
|
22 |
b64 = base64.b64encode(csv_string.encode()).decode()
|
23 |
href = f'data:text/csv;base64,{b64}'
|
24 |
-
return href
|
25 |
|
26 |
DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
|
27 |
### <center>A personal Assistant of Tony Stark for YOU
|
@@ -59,12 +137,10 @@ with gr.Blocks(css="style.css") as demo:
|
|
59 |
download_button = gr.Button("Download History")
|
60 |
download_link = gr.HTML()
|
61 |
|
62 |
-
demo.load(fn=lambda: gr.update(visible=True), outputs=[download_button])
|
63 |
-
|
64 |
def process_audio(audio, model, seed):
|
65 |
response = asyncio.run(respond(audio, model, seed))
|
66 |
-
return
|
67 |
-
|
68 |
input_audio.change(
|
69 |
fn=process_audio,
|
70 |
inputs=[input_audio, select, seed],
|
|
|
13 |
import base64
|
14 |
import io
|
15 |
|
16 |
+
default_lang = "en"
|
17 |
+
engines = { default_lang: Model(default_lang) }
|
18 |
+
|
19 |
+
def transcribe(audio):
|
20 |
+
lang = "en"
|
21 |
+
model = engines[lang]
|
22 |
+
text = model.stt_file(audio)[0]
|
23 |
+
return text
|
24 |
+
|
25 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
26 |
+
|
27 |
+
def client_fn(model):
|
28 |
+
if "Mixtral" in model:
|
29 |
+
return InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
30 |
+
elif "Llama" in model:
|
31 |
+
return InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
|
32 |
+
elif "Mistral" in model:
|
33 |
+
return InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
|
34 |
+
elif "Phi" in model:
|
35 |
+
return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
|
36 |
+
else:
|
37 |
+
return InferenceClient("microsoft/Phi-3-mini-4k-instruct")
|
38 |
+
|
39 |
+
def randomize_seed_fn(seed: int) -> int:
|
40 |
+
seed = random.randint(0, 999999)
|
41 |
+
return seed
|
42 |
+
|
43 |
+
system_instructions1 = """
|
44 |
+
[SYSTEM] Answer as Real Jarvis JARVIS, Made by 'Tony Stark.'
|
45 |
+
Keep conversation friendly, short, clear, and concise.
|
46 |
+
Avoid unnecessary introductions and answer the user's questions directly.
|
47 |
+
Respond in a normal, conversational manner while being friendly and helpful.
|
48 |
+
[USER]
|
49 |
+
"""
|
50 |
+
|
51 |
+
# Initialize an empty DataFrame to store the history
|
52 |
+
history_df = pd.DataFrame(columns=['Timestamp', 'Request', 'Response'])
|
53 |
+
|
54 |
+
def models(text, model="Mixtral 8x7B", seed=42):
|
55 |
+
global history_df
|
56 |
+
|
57 |
+
seed = int(randomize_seed_fn(seed))
|
58 |
+
generator = torch.Generator().manual_seed(seed)
|
59 |
+
|
60 |
+
client = client_fn(model)
|
61 |
+
|
62 |
+
generate_kwargs = dict(
|
63 |
+
max_new_tokens=300,
|
64 |
+
seed=seed
|
65 |
+
)
|
66 |
+
formatted_prompt = system_instructions1 + text + "[JARVIS]"
|
67 |
+
stream = client.text_generation(
|
68 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
69 |
+
output = ""
|
70 |
+
for response in stream:
|
71 |
+
if not response.token.text == "</s>":
|
72 |
+
output += response.token.text
|
73 |
+
|
74 |
+
# Add the current interaction to the history DataFrame
|
75 |
+
new_row = pd.DataFrame({
|
76 |
+
'Timestamp': [datetime.now().strftime("%Y-%m-%d %H:%M:%S")], # Convert to string
|
77 |
+
'Request': [text],
|
78 |
+
'Response': [output]
|
79 |
+
})
|
80 |
+
history_df = pd.concat([history_df, new_row], ignore_index=True)
|
81 |
+
|
82 |
+
return output
|
83 |
+
|
84 |
+
async def respond(audio, model, seed):
|
85 |
+
user = transcribe(audio)
|
86 |
+
reply = models(user, model, seed)
|
87 |
+
communicate = edge_tts.Communicate(reply)
|
88 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
89 |
+
tmp_path = tmp_file.name
|
90 |
+
await communicate.save(tmp_path)
|
91 |
+
return tmp_path
|
92 |
+
|
93 |
+
def display_history():
|
94 |
+
return history_df
|
95 |
|
96 |
def download_history():
|
97 |
csv_buffer = io.StringIO()
|
|
|
99 |
csv_string = csv_buffer.getvalue()
|
100 |
b64 = base64.b64encode(csv_string.encode()).decode()
|
101 |
href = f'data:text/csv;base64,{b64}'
|
102 |
+
return gr.HTML(f'<a href="{href}" download="chat_history.csv">Download Chat History</a>')
|
103 |
|
104 |
DESCRIPTION = """ # <center><b>JARVIS⚡</b></center>
|
105 |
### <center>A personal Assistant of Tony Stark for YOU
|
|
|
137 |
download_button = gr.Button("Download History")
|
138 |
download_link = gr.HTML()
|
139 |
|
|
|
|
|
140 |
def process_audio(audio, model, seed):
|
141 |
response = asyncio.run(respond(audio, model, seed))
|
142 |
+
return response
|
143 |
+
|
144 |
input_audio.change(
|
145 |
fn=process_audio,
|
146 |
inputs=[input_audio, select, seed],
|