kaoruhotarubi commited on
Commit
c93cae7
·
1 Parent(s): 5362889

reverted changes

Browse files
Files changed (2) hide show
  1. app.py +8 -65
  2. requirements.txt +1 -5
app.py CHANGED
@@ -1,38 +1,14 @@
1
- import subprocess
2
- import os
3
- import sys
4
  import spaces
5
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
6
  import torch
7
- import numpy as np
8
- import soundfile as sf
9
- from huggingface_hub import hf_hub_download
10
  import gradio as gr
11
  from PIL import Image
 
12
  import random
13
  import re
14
-
15
- subprocess.run(["pip", "install", "-U", "scipy", "soundfile", "torch", "numpy", "gradio", "huggingface_hub", "Cython"])
16
- # Clone VITS if not already present
17
- if not os.path.exists("vits"):
18
- subprocess.run(["git", "clone", "https://github.com/jaywalnut310/vits.git"])
19
-
20
- # Add VITS directory to the Python path
21
- sys.path.append("vits")
22
-
23
- monotonic_align_path = "vits/monotonic_align"
24
- if not os.path.exists(f"{monotonic_align_path}/monotonic_align/core.so"):
25
- print("🔧 Compiling monotonic_align...")
26
- subprocess.run(["python3", "setup.py", "build_ext", "--inplace"], cwd=monotonic_align_path, check=True)
27
-
28
- # Import VITS modules
29
- from vits.models import SynthesizerTrn
30
- from vits.utils import load_checkpoint
31
- from vits.text import text_to_sequence
32
- from vits.text.symbols import symbols # Ensure symbols are imported for text processing
33
-
34
- # Clear unused GPU memory
35
- torch.cuda.empty_cache()
36
 
37
  # Define the model name
38
  OUTPUT_DIR = "output"
@@ -40,14 +16,6 @@ os.makedirs(OUTPUT_DIR, exist_ok=True)
40
 
41
  model_name = "TheBloke/Amethyst-13B-Mistral-AWQ"
42
 
43
- # Load the TTS model
44
- model_path = hf_hub_download(repo_id="Lycoris53/Vits-TTS-Japanese-Only-Sakura-Miko", filename="G_SakuraMiko.pth")
45
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
46
-
47
- # Initialize VITS model
48
- model = SynthesizerTrn(256, 512, 1024, 32, 5).to(device)
49
- load_checkpoint(model, model_path) # Corrected argument order
50
-
51
  # Load the tokenizer
52
  tokenizer = AutoTokenizer.from_pretrained(model_name)
53
 
@@ -214,19 +182,6 @@ def remove_emojis(text):
214
 
215
  concerned_streak = 0
216
 
217
- def generate_speech(text):
218
- text_norm = text_to_sequence(text)
219
- text_tensor = torch.LongTensor(text_norm).unsqueeze(0).to(device)
220
-
221
- with torch.no_grad():
222
- audio = model.infer(text_tensor)
223
-
224
- # Save as WAV
225
- wav_path = "output.wav"
226
- sf.write(wav_path, np.array(audio.cpu().detach()), samplerate=22050)
227
-
228
- return wav_path
229
-
230
  @spaces.GPU
231
  def chat(input_text):
232
  global conversation_history, current_emotion, previous_emotion, concerned_streak
@@ -352,24 +307,13 @@ Rena:"""
352
  # ✅ Handle fallback if response is empty
353
  if not response.strip():
354
  response = "Hmm, I’m not sure how to respond to that. Can you try rephrasing?"
355
- audio_file = generate_speech(response)
356
  # ✅ Add Rena's response to the conversation history
357
  conversation_history.append(f"Rena: {response}")
358
 
359
- return response, avatar_image, audio_file
360
-
361
- def generate_speech(text):
362
- text_norm = text_to_sequence(text)
363
- text_tensor = torch.LongTensor(text_norm).unsqueeze(0).to(device)
364
-
365
- with torch.no_grad():
366
- audio = model.infer(text_tensor)
367
 
368
- # Save as WAV
369
- wav_path = "output.wav"
370
- sf.write(wav_path, np.array(audio.cpu().detach()), samplerate=22050)
371
 
372
- return wav_path
373
 
374
 
375
 
@@ -397,14 +341,13 @@ with gr.Blocks(css=css) as interface:
397
  with gr.Row():
398
  user_input = gr.Textbox(label="Your Message", lines=2, interactive=True)
399
  rena_response = gr.Textbox(label="Rena's Response", lines=10, interactive=False)
400
- tts_output = gr.Audio(label="Rena's Voice", interactive=False) # Added TTS output
401
 
402
  # Add event to handle `Enter` key press
403
- user_input.submit(chat, inputs=[user_input], outputs=[rena_response, avatar, tts_output])
404
 
405
  # Submit button (optional)
406
  submit_button = gr.Button("Submit")
407
- submit_button.click(chat, inputs=[user_input], outputs=[rena_response, avatar, tts_output])
408
 
409
  # Launch the app
410
  interface.launch()
 
 
 
 
1
  import spaces
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import torch
 
 
 
4
  import gradio as gr
5
  from PIL import Image
6
+ import os
7
  import random
8
  import re
9
+ import subprocess
10
+ torch.cuda.empty_cache() # Clears unused GPU memory
11
+ torch.cuda.memory_allocated() # Checks available GPU memory
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Define the model name
14
  OUTPUT_DIR = "output"
 
16
 
17
  model_name = "TheBloke/Amethyst-13B-Mistral-AWQ"
18
 
 
 
 
 
 
 
 
 
19
  # Load the tokenizer
20
  tokenizer = AutoTokenizer.from_pretrained(model_name)
21
 
 
182
 
183
  concerned_streak = 0
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
  @spaces.GPU
186
  def chat(input_text):
187
  global conversation_history, current_emotion, previous_emotion, concerned_streak
 
307
  # ✅ Handle fallback if response is empty
308
  if not response.strip():
309
  response = "Hmm, I’m not sure how to respond to that. Can you try rephrasing?"
310
+
311
  # ✅ Add Rena's response to the conversation history
312
  conversation_history.append(f"Rena: {response}")
313
 
314
+ return response, avatar_image
 
 
 
 
 
 
 
315
 
 
 
 
316
 
 
317
 
318
 
319
 
 
341
  with gr.Row():
342
  user_input = gr.Textbox(label="Your Message", lines=2, interactive=True)
343
  rena_response = gr.Textbox(label="Rena's Response", lines=10, interactive=False)
 
344
 
345
  # Add event to handle `Enter` key press
346
+ user_input.submit(chat, inputs=[user_input], outputs=[rena_response, avatar])
347
 
348
  # Submit button (optional)
349
  submit_button = gr.Button("Submit")
350
+ submit_button.click(chat, inputs=[user_input], outputs=[rena_response, avatar])
351
 
352
  # Launch the app
353
  interface.launch()
requirements.txt CHANGED
@@ -8,11 +8,7 @@ bitsandbytes
8
  autoawq
9
  transformers>=4.37.0
10
  triton
11
- torchaudio
12
- numpy
13
- soundfile
14
- matplotlib
15
- scipy
16
 
17
 
18
 
 
8
  autoawq
9
  transformers>=4.37.0
10
  triton
11
+
 
 
 
 
12
 
13
 
14