sujalrajpoot commited on
Commit
e8f3ace
·
verified ·
1 Parent(s): 8abecce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -22
app.py CHANGED
@@ -2,7 +2,9 @@ import os
2
  import json
3
  import logging
4
  import requests
 
5
  from uuid import uuid4
 
6
  from flask import Flask, request, Response, jsonify, send_from_directory
7
  from webscout.Provider.Deepinfra import DeepInfra
8
 
@@ -28,39 +30,114 @@ BASE_MODEL = DeepInfra(is_conversation=False, update_file=False, system_prompt=S
28
 
29
 
30
  # -------------------- TTS Generator --------------------
31
- def generate_tts(text):
32
- try:
33
- headers = {
34
- 'sec-ch-ua-platform': '"Windows"',
35
- 'Referer': 'https://www.openai.fm/',
36
- 'sec-ch-ua': '"Microsoft Edge";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
37
- 'sec-ch-ua-mobile': '?0',
38
- 'User-Agent': 'Mozilla/5.0',
39
- 'DNT': '1',
40
- 'Range': 'bytes=0-',
41
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  params = {
43
- 'input': text,
44
  'prompt': VOICE,
45
  'voice': 'alloy',
46
- 'generation': str(uuid4())
47
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- response = requests.get('https://www.openai.fm/api/generate', params=params, headers=headers)
50
- if response.status_code == 200:
51
  filename = f"{uuid4().hex}.mp3"
52
  filepath = os.path.join(AUDIO_DIR, filename)
53
- with open(filepath, 'wb') as f:
54
- f.write(response.content)
55
- logger.info(f"TTS audio generated: {filename}")
 
 
 
56
  return f"static/audio/{filename}"
57
- else:
58
- logger.warning(f"TTS failed with status {response.status_code}: {response.text}")
59
  except Exception as e:
60
- logger.exception("TTS generation error")
61
  return None
62
 
63
-
64
  # -------------------- Chat Route --------------------
65
  @app.route("/chat", methods=["POST"])
66
  def chat():
 
2
  import json
3
  import logging
4
  import requests
5
+ from typing import Dict
6
  from uuid import uuid4
7
+ from concurrent.futures import ThreadPoolExecutor, as_completed
8
  from flask import Flask, request, Response, jsonify, send_from_directory
9
  from webscout.Provider.Deepinfra import DeepInfra
10
 
 
30
 
31
 
32
  # -------------------- TTS Generator --------------------
33
+
34
+ def generate_tts(text:str, verbose:bool = True):
35
+ PROVIDER_URL: str = "https://www.openai.fm/api/generate"
36
+ headers = {
37
+ 'sec-ch-ua-platform': '"Windows"',
38
+ 'Referer': 'https://www.openai.fm/',
39
+ 'sec-ch-ua': '"Microsoft Edge";v="137", "Chromium";v="137", "Not/A)Brand";v="24"',
40
+ 'sec-ch-ua-mobile': '?0',
41
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0',
42
+ 'DNT': '1',
43
+ 'Range': 'bytes=0-',
44
+ }
45
+
46
+ def _generate_audio_chunk(text: str, chunk_number: int) -> tuple[int, bytes]:
47
+ """
48
+ Generate audio for a single text chunk.
49
+
50
+ Args:
51
+ text (str): The text chunk to convert.
52
+ chunk_number (int): The sequence number of the chunk.
53
+
54
+ Returns:
55
+ tuple[int, bytes]: Chunk number and audio data.
56
+
57
+ Raises:
58
+ TTSRequestError: If the request fails.
59
+ """
60
  params = {
61
+ 'input': text.strip(),
62
  'prompt': VOICE,
63
  'voice': 'alloy',
64
+ 'generation': str(uuid4()),
65
  }
66
+ if verbose:logger.info(f"Processing Text: {params['input'][:70]}...")
67
+ while True:
68
+ try:
69
+ response = requests.get(
70
+ PROVIDER_URL,
71
+ params=params,
72
+ headers=headers
73
+ )
74
+ response.raise_for_status()
75
+
76
+ if response.content:
77
+ if verbose:
78
+ logger.info(f"Chunk {chunk_number} processed successfully.")
79
+ return chunk_number, response.content
80
+
81
+ if verbose:
82
+ logger.info(f"No data received for chunk {chunk_number}.")
83
+
84
+ except Exception as e:
85
+ if verbose:
86
+ logger.info(f"Error processing chunk {chunk_number}: {e}")
87
+ time.sleep(1)
88
+
89
+ def _split_text_by_fullstop(text:str, max_length:int = 800):
90
+ parts = []
91
+ while len(text) > max_length:
92
+ # Find the last period (.) within the first max_length characters
93
+ split_index = text.rfind('.', 0, max_length)
94
+ if split_index == -1:
95
+ # If there's no full stop, force split at max_length
96
+ split_index = max_length
97
+ else:
98
+ split_index += 1 # Include the period in the current part
99
+
100
+ part = text[:split_index].strip()
101
+ parts.append(part)
102
+ text = text[split_index:].strip()
103
+ if text:
104
+ parts.append(text)
105
+ return parts
106
+
107
+ sentences = _split_text_by_fullstop(text)
108
+
109
+ try:
110
+ with ThreadPoolExecutor() as executor:
111
+ futures = {
112
+ executor.submit(_generate_audio_chunk, sentence.strip(), i): i
113
+ for i, sentence in enumerate(sentences, start=1)
114
+ }
115
+
116
+ audio_chunks: Dict[int, bytes] = {}
117
+
118
+ for future in as_completed(futures):
119
+ chunk_num = futures[future]
120
+ try:
121
+ part_number, audio_data = future.result()
122
+ audio_chunks[part_number] = audio_data
123
+ except Exception as e:
124
+ if verbose:
125
+ logger.info(f"Failed to generate audio for chunk {chunk_num}: {e}")
126
 
 
 
127
  filename = f"{uuid4().hex}.mp3"
128
  filepath = os.path.join(AUDIO_DIR, filename)
129
+ with filepath.open('wb') as f:
130
+ for chunk_num in sorted(audio_chunks.keys()):
131
+ f.write(audio_chunks[chunk_num])
132
+
133
+ if verbose:
134
+ logger.info(f"TTS audio generated: {filename}")
135
  return f"static/audio/{filename}"
136
+
 
137
  except Exception as e:
138
+ logger.exception(f"Failed to generate audio: {e}")
139
  return None
140
 
 
141
  # -------------------- Chat Route --------------------
142
  @app.route("/chat", methods=["POST"])
143
  def chat():