seawolf2357 commited on
Commit
4dbed2c
โ€ข
1 Parent(s): 25aed91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -15
app.py CHANGED
@@ -2,11 +2,11 @@ import os
2
  import asyncio
3
  import tempfile
4
  from gtts import gTTS
 
5
  import gradio as gr
6
  from huggingface_hub import InferenceClient
7
 
8
- DESCRIPTION = """ # <center><b>โšก์ž๋น„์Šค</b></center>
9
- """
10
 
11
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
12
  system_instructions = "[INST] ๋‹ต๋ณ€ ๋“œ๋ฆด๊ป˜์š”, '๋ฐ˜๋“œ์‹œ ํ•œ๊ตญ์–ด(ํ•œ๊ธ€)๋กœ ๋‹ต๋ณ€ํ•˜๋ผ', Keep conversation very short, clear, friendly and concise."
@@ -27,32 +27,37 @@ async def generate(prompt):
27
  for response in stream:
28
  output += response.token.text
29
 
30
- # "์˜ˆ์Šค" ๋ฌธ์ž์—ด์ด ๋งˆ์ง€๋ง‰์— ์žˆ์œผ๋ฉด ์ œ๊ฑฐ
31
- if output.strip().endswith("s"):
32
- output = output[:output.rfind("s")].strip()
33
-
34
- # ์˜ค๋””์˜ค ํŒŒ์ผ ์ƒ์„ฑ
35
  tts = gTTS(text=output, lang='ko')
36
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
37
  tts.save(tmp_file.name)
 
 
 
 
 
 
 
 
 
 
 
 
38
  tmp_path = tmp_file.name
39
 
40
  yield tmp_path
41
 
42
-
43
- with gr.Blocks(css="style.css") as demo:
44
  gr.Markdown(DESCRIPTION)
45
  with gr.Row():
46
  user_input = gr.Textbox(label="Prompt")
47
- input_text = gr.Textbox(label="Input Text", elem_id="important")
48
  output_audio = gr.Audio(label="Audio", type="filepath",
49
- interactive=False,
50
- autoplay=True,
51
- elem_classes="audio")
52
  with gr.Row():
53
  translate_btn = gr.Button("Response")
54
  translate_btn.click(fn=generate, inputs=user_input,
55
- outputs=output_audio, api_name="translate")
56
 
57
  if __name__ == "__main__":
58
- demo.queue(max_size=20).launch()
 
2
  import asyncio
3
  import tempfile
4
  from gtts import gTTS
5
+ from pydub import AudioSegment
6
  import gradio as gr
7
  from huggingface_hub import InferenceClient
8
 
9
+ DESCRIPTION = """# <center><b>โšก์ž๋น„์Šค</b></center>"""
 
10
 
11
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
12
  system_instructions = "[INST] ๋‹ต๋ณ€ ๋“œ๋ฆด๊ป˜์š”, '๋ฐ˜๋“œ์‹œ ํ•œ๊ตญ์–ด(ํ•œ๊ธ€)๋กœ ๋‹ต๋ณ€ํ•˜๋ผ', Keep conversation very short, clear, friendly and concise."
 
27
  for response in stream:
28
  output += response.token.text
29
 
 
 
 
 
 
30
  tts = gTTS(text=output, lang='ko')
31
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
32
  tts.save(tmp_file.name)
33
+
34
+ # Load the saved audio file
35
+ audio = AudioSegment.from_mp3(tmp_file.name)
36
+
37
+ # Trim the last 1 second from the audio
38
+ cut_duration = 1000 # milliseconds
39
+ if len(audio) > cut_duration:
40
+ audio = audio[:-cut_duration]
41
+
42
+ # Save the trimmed audio back to the temporary file
43
+ audio.export(tmp_file.name, format="mp3")
44
+
45
  tmp_path = tmp_file.name
46
 
47
  yield tmp_path
48
 
49
+ with gr.Blocks(css="style.css") as demo:
 
50
  gr.Markdown(DESCRIPTION)
51
  with gr.Row():
52
  user_input = gr.Textbox(label="Prompt")
 
53
  output_audio = gr.Audio(label="Audio", type="filepath",
54
+ interactive=False,
55
+ autoplay=True,
56
+ elem_classes="audio")
57
  with gr.Row():
58
  translate_btn = gr.Button("Response")
59
  translate_btn.click(fn=generate, inputs=user_input,
60
+ outputs=output_audio, api_name="translate")
61
 
62
  if __name__ == "__main__":
63
+ demo.queue(max_size=20).launch()