Hyprlyf commited on
Commit
2a31955
·
verified ·
1 Parent(s): cf085d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -52
app.py CHANGED
@@ -1,59 +1,65 @@
1
  import gradio as gr
2
- from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
3
  import torch
4
- import soundfile as sf
5
- import numpy as np
6
 
7
- # Device configuration
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
- # Supported languages (for dropdown, Roman transliteration recommended for non-English)
11
- languages = ["English", "Hindi", "Urdu", "Arabic", "Turkish", "Persian", "Malay",
12
- "Swahili", "Bengali", "Hausa"]
13
-
14
- # Load model & processor once
15
- model_name = "microsoft/speecht5_tts"
16
- processor = SpeechT5Processor.from_pretrained(model_name)
17
- model = SpeechT5ForTextToSpeech.from_pretrained(model_name).to(device)
18
- vocoder = SpeechT5HifiGan.from_pretrained(model_name).to(device)
19
-
20
- # Fixed random speaker embedding for demo
21
- speaker_embedding = torch.randn(1, 512).to(device)
22
-
23
- # Text-to-Speech function
24
- def text_to_speech(text, language):
25
- """
26
- Convert text to speech using SpeechT5 model.
27
- For non-English languages, Roman transliteration is recommended.
28
- """
29
- inputs = processor(text=text, return_tensors="pt").to(device)
30
-
31
- with torch.no_grad():
32
- # Generate mel-spectrogram
33
- speech = model.generate_speech(inputs["input_ids"], speaker_embeddings=speaker_embedding)
34
- # Convert mel to waveform
35
- audio_waveform = vocoder(speech.squeeze(0))
36
-
37
- # Convert to 1D numpy float32 for Gradio compatibility
38
- audio_np = audio_waveform.squeeze().cpu().numpy().astype(np.float32)
39
- samplerate = processor.feature_extractor.sampling_rate
40
-
41
- # Optional: save output
42
- sf.write("output.wav", audio_np, samplerate)
43
-
44
- return (audio_np, samplerate)
45
-
46
- # Gradio Interface
47
- iface = gr.Interface(
48
- fn=text_to_speech,
49
- inputs=[
50
- gr.Textbox(lines=3, placeholder="Type your text here..."),
51
- gr.Dropdown(languages, label="Select Language")
52
- ],
53
- outputs=gr.Audio(type="numpy", autoplay=True),
54
- title="Multi-Language TTS (SpeechT5)",
55
- description="Type text and select language. Roman transliteration recommended for non-English languages."
56
  )
57
 
58
- # Launch
59
- iface.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
 
 
4
 
5
+ # Device
6
  device = "cuda" if torch.cuda.is_available() else "cpu"
7
 
8
+ # Load Model
9
+ model_name = "Hyprlyf/hypr1-instruct"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ model_name,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
14
+ device_map="auto"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
 
17
+ # Chat function
18
+ def chat_with_model(user_input, history=[]):
19
+ # Combine history into context
20
+ context = ""
21
+ for h in history:
22
+ context += f"User: {h[0]}\nAssistant: {h[1]}\n"
23
+ context += f"User: {user_input}\nAssistant:"
24
+
25
+ inputs = tokenizer(context, return_tensors="pt").to(device)
26
+
27
+ with torch.no_grad():
28
+ outputs = model.generate(
29
+ **inputs,
30
+ max_new_tokens=256,
31
+ temperature=0.7,
32
+ top_p=0.9,
33
+ do_sample=True,
34
+ pad_token_id=tokenizer.eos_token_id
35
+ )
36
+
37
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
38
+
39
+ # Extract only assistant's last reply
40
+ if "Assistant:" in response:
41
+ reply = response.split("Assistant:")[-1].strip()
42
+ else:
43
+ reply = response.strip()
44
+
45
+ history.append((user_input, reply))
46
+ return history, history
47
+
48
+ # Gradio Chatbot UI
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("# 🤖 Hyprlyf/hypr1-instruct Chatbot")
51
+
52
+ chatbot = gr.Chatbot()
53
+ msg = gr.Textbox(placeholder="Type your message here...")
54
+ clear = gr.Button("Clear")
55
+
56
+ state = gr.State([])
57
+
58
+ def respond(message, state):
59
+ state, updated_history = chat_with_model(message, state)
60
+ return updated_history, state
61
+
62
+ msg.submit(respond, [msg, state], [chatbot, state])
63
+ clear.click(lambda: ([], []), None, [chatbot, state])
64
+
65
+ demo.launch(share=True)