Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,9 @@ LLM_MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
|
|
| 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
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
# Create inference client for text generation
|
| 21 |
client = InferenceClient(LLM_MODEL_NAME)
|
|
@@ -51,7 +51,9 @@ def generate(instruct_history, temperature=0.1, max_new_tokens=128, top_p=0.95,
|
|
| 51 |
return output
|
| 52 |
|
| 53 |
@spaces.GPU(duration=60)
|
| 54 |
-
def transcribe(audio,
|
|
|
|
|
|
|
| 55 |
sr, y = audio
|
| 56 |
y = y.astype(np.float32)
|
| 57 |
y /= np.max(np.abs(y))
|
|
@@ -60,7 +62,8 @@ def transcribe(audio, instruct_history, formatted_history):
|
|
| 60 |
transcribed_user_audio = pipe({"sampling_rate": sr, "raw": y})["text"]
|
| 61 |
|
| 62 |
# Append user input to history
|
| 63 |
-
formatted_history +=
|
|
|
|
| 64 |
instruct_history += f"<s>[INST] {transcribed_user_audio} [/INST] "
|
| 65 |
|
| 66 |
# Generate LLM response
|
|
@@ -68,21 +71,19 @@ def transcribe(audio, instruct_history, formatted_history):
|
|
| 68 |
|
| 69 |
# Append AI response to history
|
| 70 |
instruct_history += f" {llm_response}</s>"
|
| 71 |
-
formatted_history += f"Friday: {llm_response}\n\n"
|
| 72 |
|
| 73 |
# Convert AI response to audio
|
| 74 |
audio_response = gTTS(llm_response)
|
| 75 |
audio_response.save("response.mp3")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
return "response.mp3", formatted_history, instruct_history
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
formatted_state = gr.State(value=initial_formatted_history)
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
audio_input = gr.Audio(label="Human", sources="microphone")
|
|
@@ -93,13 +94,8 @@ with gr.Blocks() as demo:
|
|
| 93 |
# Textbox to display the full conversation history
|
| 94 |
transcription_box = gr.Textbox(label="Transcription", lines=10, placeholder="Conversation History...")
|
| 95 |
|
| 96 |
-
|
| 97 |
-
transcribe_btn.click(
|
| 98 |
-
fn=transcribe,
|
| 99 |
-
inputs=[audio_input, instruct_state, formatted_state],
|
| 100 |
-
outputs=[output_audio, transcription_box, instruct_state, formatted_state]
|
| 101 |
-
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.queue()
|
| 105 |
-
demo.launch()
|
|
|
|
| 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)
|
|
|
|
| 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))
|
|
|
|
| 62 |
transcribed_user_audio = pipe({"sampling_rate": sr, "raw": y})["text"]
|
| 63 |
|
| 64 |
# Append user input to history
|
| 65 |
+
formatted_history += past_history
|
| 66 |
+
formatted_history += f"π Human: {transcribed_user_audio}\n\n"
|
| 67 |
instruct_history += f"<s>[INST] {transcribed_user_audio} [/INST] "
|
| 68 |
|
| 69 |
# Generate LLM response
|
|
|
|
| 71 |
|
| 72 |
# Append AI response to history
|
| 73 |
instruct_history += f" {llm_response}</s>"
|
| 74 |
+
formatted_history += f"π€ Friday: {llm_response}\n\n"
|
| 75 |
|
| 76 |
# Convert AI response to audio
|
| 77 |
audio_response = gTTS(llm_response)
|
| 78 |
audio_response.save("response.mp3")
|
| 79 |
|
| 80 |
+
print("Formatted History: ", formatted_history)
|
|
|
|
| 81 |
|
| 82 |
+
# Return the full conversation history
|
| 83 |
+
return "response.mp3", formatted_history
|
| 84 |
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
gr.HTML("<center><h1>Friday: AI Virtual Assistant π€</h1><center>")
|
|
|
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
audio_input = gr.Audio(label="Human", sources="microphone")
|
|
|
|
| 94 |
# Textbox to display the full conversation history
|
| 95 |
transcription_box = gr.Textbox(label="Transcription", lines=10, placeholder="Conversation History...")
|
| 96 |
|
| 97 |
+
transcribe_btn.click(fn=transcribe, inputs=[audio_input, transcription_box], outputs=[output_audio, transcription_box])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.queue()
|
| 101 |
+
demo.launch()
|