Turbiling commited on
Commit
593b83c
·
verified ·
1 Parent(s): 3f39059

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -18,9 +18,8 @@ from groq import Groq
18
 
19
  client = Groq(api_key=os.getenv("GROQ_API_KEY"))
20
 
21
- TRANSCRIPTION_MODEL = "turbowhisper-large-v3" # updated model
22
- GRAMMAR_MODEL = "openai/gpt-oss-120b" # grammar & punctuation fix
23
-
24
  CHUNK_SIZE = 600 # 10 min in seconds
25
 
26
  # ============================================================================
@@ -28,7 +27,7 @@ CHUNK_SIZE = 600 # 10 min in seconds
28
  # ============================================================================
29
 
30
  def get_video_duration(video_path):
31
- """Get video duration using ffprobe (more reliable than imageio)"""
32
  try:
33
  result = subprocess.run(
34
  ["ffprobe", "-v", "error", "-show_entries",
@@ -100,41 +99,46 @@ def transcribe_audio_chunks(audio_chunks, language="English"):
100
  return transcript_text.strip()
101
 
102
  # ============================================================================
103
- # GRAMMAR & PUNCTUATION FIX
104
  # ============================================================================
105
 
106
- def correct_grammar(text, language="English"):
107
- """Use Groq GPT model to fix grammar and punctuation"""
108
  try:
109
- prompt = f"Correct the grammar and punctuation for the following text in {language}:\n\n{text}"
110
  response = client.chat.completions.create(
111
- model=GRAMMAR_MODEL,
112
- messages=[{"role": "user", "content": prompt}],
113
  temperature=0
114
  )
115
- fixed_text = response.choices[0].message.content
116
- return fixed_text
117
  except Exception as e:
118
- return f"[Grammar correction failed: {str(e)}]"
119
 
120
  # ============================================================================
121
  # PROCESS VIDEO
122
  # ============================================================================
123
 
124
- def process_video_ui(video_file, language):
125
  lang = "English" if "English" in language else "Urdu"
126
  if not video_file:
127
  return "", "Please upload a video file." if lang=="English" else "براہ کرم ایک ویڈیو فائل اپ لوڈ کریں۔"
128
  try:
129
  audio_chunks = extract_audio_chunks(video_file.name)
130
  transcript = transcribe_audio_chunks(audio_chunks, language=lang)
131
- # fix grammar & punctuation
132
- transcript_fixed = correct_grammar(transcript, language=lang)
133
- return transcript_fixed, "✅ Success / کامیاب"
134
  except Exception as e:
135
  error_msg = str(e)
136
  return "", error_msg if lang=="English" else "خرابی: " + error_msg
137
 
 
 
 
 
 
 
 
138
  # ============================================================================
139
  # GRADIO UI
140
  # ============================================================================
@@ -142,17 +146,30 @@ def process_video_ui(video_file, language):
142
  def create_ui():
143
  with gr.Blocks(title="AI Graduate Job Matcher") as demo:
144
  gr.HTML("<h1>🎓 AI Graduate Job-Matching Platform</h1><p>Developer: Najaf Ali Sharqi</p>")
 
145
  video_input = gr.File(label="Upload Video / ویڈیو اپ لوڈ کریں", file_types=[".mp4", ".mov", ".avi"])
146
  language_dropdown = gr.Dropdown(choices=["English", "Urdu / اردو"], value="English", label="Output Language / آؤٹ پٹ زبان")
147
- process_btn = gr.Button("🚀 Generate Transcript / ٹرانسکرپٹ بنائیں")
148
  transcript_output = gr.Textbox(label="Transcript / ٹرانسکرپٹ", lines=20)
149
  status_output = gr.Textbox(label="Status / حیثیت", lines=1)
150
 
151
- process_btn.click(
152
- fn=process_video_ui,
 
 
 
 
 
153
  inputs=[video_input, language_dropdown],
154
  outputs=[transcript_output, status_output]
155
  )
 
 
 
 
 
 
 
156
 
157
  return demo
158
 
 
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
24
 
25
  # ============================================================================
 
27
  # ============================================================================
28
 
29
  def get_video_duration(video_path):
30
+ """Get video duration using ffprobe"""
31
  try:
32
  result = subprocess.run(
33
  ["ffprobe", "-v", "error", "-show_entries",
 
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
  # ============================================================================
122
 
123
+ def generate_transcript(video_file, language):
124
  lang = "English" if "English" in language else "Urdu"
125
  if not video_file:
126
  return "", "Please upload a video file." if lang=="English" else "براہ کرم ایک ویڈیو فائل اپ لوڈ کریں۔"
127
  try:
128
  audio_chunks = extract_audio_chunks(video_file.name)
129
  transcript = transcribe_audio_chunks(audio_chunks, language=lang)
130
+ return transcript, "✅ Transcription completed / ٹرانسکرپشن مکمل"
 
 
131
  except Exception as e:
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
  # GRADIO UI
144
  # ============================================================================
 
146
  def create_ui():
147
  with gr.Blocks(title="AI Graduate Job Matcher") as demo:
148
  gr.HTML("<h1>🎓 AI Graduate Job-Matching Platform</h1><p>Developer: Najaf Ali Sharqi</p>")
149
+
150
  video_input = gr.File(label="Upload Video / ویڈیو اپ لوڈ کریں", file_types=[".mp4", ".mov", ".avi"])
151
  language_dropdown = gr.Dropdown(choices=["English", "Urdu / اردو"], value="English", label="Output Language / آؤٹ پٹ زبان")
152
+
153
  transcript_output = gr.Textbox(label="Transcript / ٹرانسکرپٹ", lines=20)
154
  status_output = gr.Textbox(label="Status / حیثیت", lines=1)
155
 
156
+ # Buttons
157
+ transcribe_btn = gr.Button("🚀 Generate Transcript / ٹرانسکرپٹ بنائیں")
158
+ rewrite_btn = gr.Button("✏️ Correct Grammar & Punctuation / گرامر درست کریں")
159
+
160
+ # Transcription click
161
+ transcribe_btn.click(
162
+ fn=generate_transcript,
163
  inputs=[video_input, language_dropdown],
164
  outputs=[transcript_output, status_output]
165
  )
166
+
167
+ # Grammar correction click
168
+ rewrite_btn.click(
169
+ fn=correct_transcript,
170
+ inputs=[transcript_output, language_dropdown],
171
+ outputs=[transcript_output, status_output]
172
+ )
173
 
174
  return demo
175