gospacedev commited on
Commit
38fd5f7
Β·
2 Parent(s): eca0c0e 70ca8ee

Merge branch 'main' of hf.co:spaces/gospacedev/friday

Browse files
Files changed (2) hide show
  1. README.md +3 -3
  2. app.py +27 -25
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.28.3
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
- 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, umm, how can I help you?</s>"""
15
-
16
- instruct_history = system_prompt + """"""
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, instruct_history=instruct_history, formatted_history=formatted_history):
 
 
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 += f"""Human: {transcribed_user_audio}\n\n"""
61
 
62
- instruct_history += f""" <s>[INST] {transcribed_user_audio} [/INST] """
 
63
 
 
64
  llm_response = generate(instruct_history)
65
 
66
- instruct_history += f""" {llm_response}</s> """
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(instruct_history)
74
 
 
75
  return "response.mp3", formatted_history
76
 
77
-
78
  with gr.Blocks() as demo:
79
- gr.HTML("<center><h1>Friday: AI Virtual Assistant<h1><center>")
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
- transcription_box = gr.Textbox(label="Transcription")
 
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()