englissi commited on
Commit
4ba54ec
Β·
verified Β·
1 Parent(s): 0ae6150

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -44
app.py CHANGED
@@ -1,50 +1,35 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
- from TTS.api import TTS
4
 
5
- # λͺ¨λΈ 이름
6
- model_name = "facebook/llama-7b"
7
 
8
- # λͺ¨λΈ 및 ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ
9
- try:
10
- tokenizer = AutoTokenizer.from_pretrained(model_name)
11
- model = AutoModelForCausalLM.from_pretrained(model_name)
12
- except Exception as e:
13
- raise RuntimeError(f"λͺ¨λΈ λ‘œλ“œ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
 
 
 
 
 
 
 
14
 
15
- # TTS λͺ¨λΈ λ‘œλ“œ
16
- try:
17
- tts = TTS(model_name="tts_models/bg/cv/vits", progress_bar=False)
18
- except Exception as e:
19
- raise RuntimeError(f"TTS λͺ¨λΈ λ‘œλ“œ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
 
 
 
 
 
 
20
 
21
- # ν…μŠ€νŠΈ 생성 및 μŒμ„± λ³€ν™˜ ν•¨μˆ˜
22
- def generate_audio(input_text):
23
- try:
24
- # ν…μŠ€νŠΈ 생성
25
- inputs = tokenizer.encode(input_text, return_tensors="pt")
26
- outputs = model.generate(inputs, max_length=100, num_return_sequences=1)
27
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
- except Exception as e:
29
- return f"ν…μŠ€νŠΈ 생성 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}", None
30
-
31
- try:
32
- # TTS λ³€ν™˜
33
- audio_path = "output.wav"
34
- tts.tts_to_file(text=generated_text, file_path=audio_path)
35
- return generated_text, audio_path
36
- except Exception as e:
37
- return f"TTS λ³€ν™˜ 쀑 λ¬Έμ œκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}", None
38
-
39
- # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
40
- interface = gr.Interface(
41
- fn=generate_audio,
42
- inputs=gr.Textbox(lines=5, label="ν…μŠ€νŠΈ μž…λ ₯"),
43
- outputs=[gr.Textbox(label="μƒμ„±λœ ν…μŠ€νŠΈ"), gr.Audio(label="μƒμ„±λœ μŒμ„±")],
44
- title="LLaMA 7B ν…μŠ€νŠΈ 생성 및 μŒμ„± λ³€ν™˜",
45
- description="ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•˜λ©΄ μƒμ„±λœ ν…μŠ€νŠΈλ₯Ό 좜λ ₯ν•˜κ³  μŒμ„±μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€."
46
- )
47
-
48
- # μ•± μ‹€ν–‰
49
  if __name__ == "__main__":
50
- interface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Step 1: Initialize the text-generation pipeline
5
+ pipe = pipeline("text-generation", model="sambanovasystems/SambaLingo-Bulgarian-Base")
6
 
7
+ # Step 2: Define a function to generate Bulgarian text and convert it to TTS
8
+ def generate_and_speak(prompt):
9
+ # Generate text using the model
10
+ generated_text = pipe(prompt, max_length=50, num_return_sequences=1)[0]["generated_text"]
11
+
12
+ # Placeholder: Simulating TTS (you can replace this with an actual TTS library)
13
+ # Install a TTS library such as gTTS for better implementation
14
+ from gtts import gTTS
15
+ tts = gTTS(generated_text, lang="bg")
16
+ audio_file = "output.mp3"
17
+ tts.save(audio_file)
18
+
19
+ return generated_text, audio_file
20
 
21
+ # Step 3: Create Gradio interface
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown("## Bulgarian Text Generator and TTS")
24
+ with gr.Row():
25
+ input_prompt = gr.Textbox(label="Enter a prompt in Bulgarian:")
26
+ with gr.Column():
27
+ output_text = gr.Textbox(label="Generated Text")
28
+ output_audio = gr.Audio(label="Generated Speech", type="file")
29
+ generate_button = gr.Button("Generate")
30
+
31
+ generate_button.click(generate_and_speak, inputs=input_prompt, outputs=[output_text, output_audio])
32
 
33
+ # Run the app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  if __name__ == "__main__":
35
+ demo.launch()