NeoPy commited on
Commit
d635cd8
Β·
verified Β·
1 Parent(s): b9ccc10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -29
app.py CHANGED
@@ -1,44 +1,119 @@
1
- import gradio as gr
2
- from gradio import Server
3
- from transformers import T5ForConditionalGeneration, T5Tokenizer
4
 
5
- # Load model and tokenizer
6
- MODEL_NAME = "alirezamsh/small100"
7
- print("Loading model...")
8
- tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME)
9
- model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME)
10
- print("Model loaded!")
11
 
12
 
13
- app = Server()
14
 
15
- @app.api(name="translate")
16
- def translate(text, prompt):
 
 
 
 
 
17
  """
18
- Translate text using T5 with a task prefix.
19
- Note: t5-small is trained mainly on English tasks and has limited
20
- multilingual translation ability. See note at the bottom.
 
 
 
 
 
 
 
 
 
 
 
 
21
  """
22
- if not text.strip():
23
- return "Please enter some text to translate."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
26
 
27
- with torch.no_grad():
28
- outputs = model.generate(
29
- **inputs,
30
- max_length=512,
31
- num_beams=4,
32
- early_stopping=True,
33
- )
34
 
35
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
 
41
- # ─────────────────────────────────────────────
42
- # 5. LAUNCH
43
  # ─────────────────────────────────────────────
44
  app.launch()
 
1
+ import requests
2
+ import urllib.parse
3
+ from typing import Optional, Dict, Any
4
 
5
+ app = Server()
 
 
 
 
 
6
 
7
 
 
8
 
9
+ def generate_tts_audio(
10
+ text: str,
11
+ voice: str = "Sam",
12
+ pitch: int = 100,
13
+ speed: int = 100,
14
+ base_url: str = "https://tetyys.com/SAPI4"
15
+ ) -> bytes:
16
  """
17
+ Generate TTS audio using the Tetyys SAPI4 web interface.
18
+
19
+ Args:
20
+ text: Text to synthesize (required)
21
+ voice: Voice name (optional, default: "Sam")
22
+ pitch: Pitch value (optional, default: 100)
23
+ speed: Speed value (optional, default: 100)
24
+ base_url: Base URL for the API (optional)
25
+
26
+ Returns:
27
+ bytes: WAV audio file content
28
+
29
+ Raises:
30
+ requests.RequestException: If the HTTP request fails
31
+ ValueError: If the response is not valid audio
32
  """
33
+ # URL encode the text to handle special characters
34
+ encoded_text = urllib.parse.quote(text)
35
+
36
+ # Build the endpoint URL with query parameters
37
+ endpoint = f"{base_url}/SAPI4?text={encoded_text}"
38
+
39
+ # Add optional parameters if provided
40
+ params = []
41
+ if voice:
42
+ params.append(f"voice={urllib.parse.quote(voice)}")
43
+ if pitch is not None:
44
+ params.append(f"pitch={pitch}")
45
+ if speed is not None:
46
+ params.append(f"speed={speed}")
47
+
48
+ # Combine base endpoint with optional parameters
49
+ if params:
50
+ endpoint += "&" + "&".join(params)
51
+
52
+ # Make the request
53
+ response = requests.get(endpoint, timeout=30)
54
+ response.raise_for_status()
55
+
56
+ # Validate that we received audio content
57
+ content_type = response.headers.get('Content-Type', '')
58
+ if 'audio' not in content_type.lower():
59
+ raise ValueError(
60
+ f"Expected audio response, got: {content_type}. "
61
+ f"Response preview: {response.text[:100]}"
62
+ )
63
+
64
+ return response.content
65
 
 
66
 
 
 
 
 
 
 
 
67
 
68
+ @app.api(name="tts")
69
+ def save_tts_audio(
70
+ text: str,
71
+ output_path: str,
72
+ voice: str = "Sam",
73
+ pitch: int = 100,
74
+ speed: int = 100,
75
+ base_url: str = "https://tetyys.com/SAPI4"
76
+ ) -> None:
77
+ """
78
+ Generate TTS audio and save it to a file.
79
+
80
+ Args:
81
+ text: Text to synthesize
82
+ output_path: Path where the WAV file will be saved
83
+ voice: Voice name (optional, default: "Sam")
84
+ pitch: Pitch value (optional, default: 100)
85
+ speed: Speed value (optional, default: 100)
86
+ base_url: Base URL for the API (optional)
87
+ """
88
+ audio_data = generate_tts_audio(text, voice, pitch, speed, base_url)
89
+
90
+ with open(output_path, 'wb') as f:
91
+ f.write(audio_data)
92
+
93
 
94
+ def get_voice_limits(
95
+ voice: str,
96
+ base_url: str = "https://tetyys.com/SAPI4"
97
+ ) -> Dict[str, Any]:
98
+ """
99
+ Get the pitch and speed limitations for a specific voice.
100
+
101
+ Args:
102
+ voice: Voice name to query
103
+ base_url: Base URL for the API (optional)
104
+
105
+ Returns:
106
+ dict: Voice limitations data
107
+ """
108
+ encoded_voice = urllib.parse.quote(voice)
109
+ endpoint = f"{base_url}/VoiceLimitations?voice={encoded_voice}"
110
+
111
+ response = requests.get(endpoint, timeout=30)
112
+ response.raise_for_status()
113
+
114
+ return response.json()
115
 
116
 
117
 
 
 
118
  # ─────────────────────────────────────────────
119
  app.launch()