Spaces:
Sleeping
Sleeping
gospacedev
commited on
Merge branch 'main' of hf.co:spaces/gospacedev/friday
Browse files
README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
---
|
2 |
title: Friday
|
3 |
-
emoji:
|
4 |
colorFrom: pink
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
title: Friday
|
3 |
+
emoji: π
|
4 |
colorFrom: pink
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.39.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
11 |
---
|
12 |
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
@@ -6,28 +6,30 @@ from gtts import gTTS
|
|
6 |
from transformers import pipeline
|
7 |
from huggingface_hub import InferenceClient
|
8 |
|
9 |
-
|
10 |
ASR_MODEL_NAME = "openai/whisper-small"
|
11 |
LLM_MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
12 |
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
formatted_history = """"""
|
19 |
|
|
|
20 |
client = InferenceClient(LLM_MODEL_NAME)
|
21 |
|
|
|
22 |
device = 0 if torch.cuda.is_available() else "cpu"
|
23 |
|
|
|
24 |
pipe = pipeline(
|
25 |
task="automatic-speech-recognition",
|
26 |
model=ASR_MODEL_NAME,
|
27 |
device=device,
|
28 |
)
|
29 |
|
30 |
-
|
31 |
def generate(instruct_history, temperature=0.1, max_new_tokens=128, top_p=0.95, repetition_penalty=1.0):
|
32 |
temperature = float(temperature)
|
33 |
if temperature < 1e-2:
|
@@ -48,50 +50,50 @@ def generate(instruct_history, temperature=0.1, max_new_tokens=128, top_p=0.95,
|
|
48 |
|
49 |
return output
|
50 |
|
51 |
-
|
52 |
@spaces.GPU(duration=60)
|
53 |
-
def transcribe(audio,
|
|
|
|
|
54 |
sr, y = audio
|
55 |
y = y.astype(np.float32)
|
56 |
y /= np.max(np.abs(y))
|
57 |
|
58 |
transcribed_user_audio = pipe({"sampling_rate": sr, "raw": y})["text"]
|
59 |
|
60 |
-
formatted_history +=
|
61 |
|
62 |
-
|
|
|
63 |
|
|
|
64 |
llm_response = generate(instruct_history)
|
65 |
|
66 |
-
instruct_history += f"
|
67 |
-
|
68 |
-
formatted_history += f"""Friday: {llm_response}\n\n"""
|
69 |
|
|
|
70 |
audio_response = gTTS(llm_response)
|
71 |
audio_response.save("response.mp3")
|
72 |
|
73 |
-
print(
|
74 |
|
|
|
75 |
return "response.mp3", formatted_history
|
76 |
|
77 |
-
|
78 |
with gr.Blocks() as demo:
|
79 |
-
gr.HTML("<center><h1>Friday: AI Virtual Assistant
|
80 |
|
81 |
with gr.Row():
|
82 |
audio_input = gr.Audio(label="Human", sources="microphone")
|
83 |
-
output_audio = gr.Audio(label="Friday", type="filepath",
|
84 |
-
interactive=False,
|
85 |
-
autoplay=True,
|
86 |
-
elem_classes="audio")
|
87 |
|
88 |
transcribe_btn = gr.Button("Transcribe")
|
89 |
|
90 |
-
|
|
|
91 |
|
92 |
-
transcribe_btn.click(fn=transcribe, inputs=[audio_input],
|
93 |
-
outputs=[output_audio, transcription_box])
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
demo.queue()
|
97 |
-
demo.launch()
|
|
|
6 |
from transformers import pipeline
|
7 |
from huggingface_hub import InferenceClient
|
8 |
|
9 |
+
# Model names
|
10 |
ASR_MODEL_NAME = "openai/whisper-small"
|
11 |
LLM_MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
12 |
|
13 |
+
# Initial system prompt
|
14 |
+
system_prompt = """"<s>[INST] You are Friday, a helpful and conversational AI assistant, and you respond with one to two sentences. [/INST] Hello there! I'm Friday, how can I help you?</s>"""
|
15 |
|
16 |
+
# Global variables for history
|
17 |
+
instruct_history = system_prompt
|
18 |
+
formatted_history = ""
|
|
|
|
|
19 |
|
20 |
+
# Create inference client for text generation
|
21 |
client = InferenceClient(LLM_MODEL_NAME)
|
22 |
|
23 |
+
# Set device for ASR pipeline
|
24 |
device = 0 if torch.cuda.is_available() else "cpu"
|
25 |
|
26 |
+
# ASR pipeline
|
27 |
pipe = pipeline(
|
28 |
task="automatic-speech-recognition",
|
29 |
model=ASR_MODEL_NAME,
|
30 |
device=device,
|
31 |
)
|
32 |
|
|
|
33 |
def generate(instruct_history, temperature=0.1, max_new_tokens=128, top_p=0.95, repetition_penalty=1.0):
|
34 |
temperature = float(temperature)
|
35 |
if temperature < 1e-2:
|
|
|
50 |
|
51 |
return output
|
52 |
|
|
|
53 |
@spaces.GPU(duration=60)
|
54 |
+
def transcribe(audio, past_history):
|
55 |
+
global instruct_history, formatted_history
|
56 |
+
|
57 |
sr, y = audio
|
58 |
y = y.astype(np.float32)
|
59 |
y /= np.max(np.abs(y))
|
60 |
|
61 |
transcribed_user_audio = pipe({"sampling_rate": sr, "raw": y})["text"]
|
62 |
|
63 |
+
formatted_history += past_history
|
64 |
|
65 |
+
formatted_history += f"π Human: {transcribed_user_audio}\n\n"
|
66 |
+
instruct_history += f"<s>[INST] {transcribed_user_audio} [/INST] "
|
67 |
|
68 |
+
# Generate LLM response
|
69 |
llm_response = generate(instruct_history)
|
70 |
|
71 |
+
instruct_history += f" {llm_response}</s>"
|
72 |
+
formatted_history += f"π€ Friday: {llm_response}\n\n"
|
|
|
73 |
|
74 |
+
# Convert AI response to audio
|
75 |
audio_response = gTTS(llm_response)
|
76 |
audio_response.save("response.mp3")
|
77 |
|
78 |
+
print("Formatted History: ", formatted_history)
|
79 |
|
80 |
+
# Return the full conversation history
|
81 |
return "response.mp3", formatted_history
|
82 |
|
|
|
83 |
with gr.Blocks() as demo:
|
84 |
+
gr.HTML("<center><h1>Friday: AI Virtual Assistant π€</h1><center>")
|
85 |
|
86 |
with gr.Row():
|
87 |
audio_input = gr.Audio(label="Human", sources="microphone")
|
88 |
+
output_audio = gr.Audio(label="Friday", type="filepath", interactive=False, autoplay=True, elem_classes="audio")
|
|
|
|
|
|
|
89 |
|
90 |
transcribe_btn = gr.Button("Transcribe")
|
91 |
|
92 |
+
# Textbox to display the full conversation history
|
93 |
+
transcription_box = gr.Textbox(label="Transcription", lines=10, placeholder="Conversation History...")
|
94 |
|
95 |
+
transcribe_btn.click(fn=transcribe, inputs=[audio_input, transcription_box], outputs=[output_audio, transcription_box])
|
|
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
demo.queue()
|
99 |
+
demo.launch()
|