Turbiling commited on
Commit
d180905
·
verified ·
1 Parent(s): 1330f1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -18,9 +18,9 @@ from groq import Groq
18
 
19
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
20
 
21
- TRANSCRIPTION_MODEL = "whisper-large-v3" # updated model
22
  REWRITE_MODEL = "openai/gpt-oss-120b"
23
- CHUNK_SIZE = 600 # 10 min in seconds
24
 
25
  # ============================================================================
26
  # UTILITY FUNCTIONS
@@ -99,23 +99,41 @@ def transcribe_audio_chunks(audio_chunks, language="English"):
99
  return transcript_text.strip()
100
 
101
  # ============================================================================
102
- # GRAMMAR & PUNCTUATION CORRECTION
103
  # ============================================================================
104
 
105
- def rewrite_transcript(text, language="English"):
106
- """Rewrite transcript using GPT-OSS-120B with correct grammar & punctuation"""
 
 
 
107
  try:
108
- prompt = f"Correct grammar, punctuation, and make it readable. Language: {language}\n\n{text}"
109
- response = client.chat.completions.create(
110
- model=REWRITE_MODEL,
111
- messages=[{"role":"user", "content": prompt}],
112
- temperature=0
113
- )
114
- rewritten_text = response.choices[0].message.content
 
 
 
 
 
 
 
 
115
  return rewritten_text
116
  except Exception as e:
117
  return f"[Error rewriting transcript: {str(e)}]"
118
 
 
 
 
 
 
 
 
119
  # ============================================================================
120
  # PROCESS VIDEO
121
  # ============================================================================
@@ -132,13 +150,6 @@ def generate_transcript(video_file, language):
132
  error_msg = str(e)
133
  return "", error_msg if lang=="English" else "خرابی: " + error_msg
134
 
135
- def correct_transcript(transcript_text, language):
136
- lang = "English" if "English" in language else "Urdu"
137
- if not transcript_text:
138
- return "", "Please generate transcript first." if lang=="English" else "پہلے ٹرانسکرپٹ بنائیں۔"
139
- rewritten_text = rewrite_transcript(transcript_text, language=lang)
140
- return rewritten_text, "✅ Grammar & punctuation corrected / گرامر اور پنکچویشن درست کی گئی"
141
-
142
  # ============================================================================
143
  # DOWNLOAD FUNCTION
144
  # ============================================================================
 
18
 
19
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
20
 
21
+ TRANSCRIPTION_MODEL = "turbowhisper-large-v3"
22
  REWRITE_MODEL = "openai/gpt-oss-120b"
23
+ CHUNK_SIZE = 600 # 10 min in seconds for audio splitting
24
 
25
  # ============================================================================
26
  # UTILITY FUNCTIONS
 
99
  return transcript_text.strip()
100
 
101
  # ============================================================================
102
+ # GRAMMAR & PUNCTUATION CORRECTION (CHUNKED)
103
  # ============================================================================
104
 
105
+ def rewrite_transcript_chunked(text, language="English", chunk_size=4000):
106
+ """
107
+ Rewrite transcript in smaller chunks to avoid token limits
108
+ chunk_size is approx number of words per request (adjust as needed)
109
+ """
110
  try:
111
+ words = text.split()
112
+ rewritten_chunks = []
113
+
114
+ for i in range(0, len(words), chunk_size):
115
+ chunk_text = " ".join(words[i:i+chunk_size])
116
+ prompt = f"Correct grammar, punctuation, and make it readable. Language: {language}\n\n{chunk_text}"
117
+ response = client.chat.completions.create(
118
+ model=REWRITE_MODEL,
119
+ messages=[{"role": "user", "content": prompt}],
120
+ temperature=0
121
+ )
122
+ rewritten_chunks.append(response.choices[0].message.content)
123
+
124
+ # Join all rewritten chunks
125
+ rewritten_text = "\n\n".join(rewritten_chunks)
126
  return rewritten_text
127
  except Exception as e:
128
  return f"[Error rewriting transcript: {str(e)}]"
129
 
130
+ def correct_transcript(transcript_text, language):
131
+ lang = "English" if "English" in language else "Urdu"
132
+ if not transcript_text:
133
+ return "", "Please generate transcript first." if lang=="English" else "پہلے ٹرانسکرپٹ بنائیں۔"
134
+ rewritten_text = rewrite_transcript_chunked(transcript_text, language=lang)
135
+ return rewritten_text, "✅ Grammar & punctuation corrected / گرامر اور پنکچویشن درست کی گئی"
136
+
137
  # ============================================================================
138
  # PROCESS VIDEO
139
  # ============================================================================
 
150
  error_msg = str(e)
151
  return "", error_msg if lang=="English" else "خرابی: " + error_msg
152
 
 
 
 
 
 
 
 
153
  # ============================================================================
154
  # DOWNLOAD FUNCTION
155
  # ============================================================================