ankban commited on
Commit
fee1ded
Β·
verified Β·
1 Parent(s): 4759fe8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -8
app.py CHANGED
@@ -8,6 +8,8 @@ from faster_whisper import WhisperModel
8
  import subprocess
9
  import shutil
10
  import datetime
 
 
11
 
12
  # === Clean /tmp at startup ===
13
  TMP_DIR = "/tmp"
@@ -46,8 +48,19 @@ LANG_CODES = {
46
  model = WhisperModel("base", compute_type="int8")
47
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
48
 
49
- # === In-memory session history ===
50
- session_history = []
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # === Audio Processing ===
53
  def convert_to_wav(input_file):
@@ -121,16 +134,30 @@ def tutor_feedback(audio_file, language):
121
  mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
122
  tts.save(mp3_path)
123
 
124
- # Store session history
125
  timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
126
- session_history.append({
127
  "timestamp": timestamp,
128
  "transcript": transcript,
129
  "feedback": feedback_text
130
- })
 
 
131
 
132
  return transcript, feedback_text, mp3_path, transcript
133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  # === Gradio Interface ===
135
  with gr.Blocks(css="light_mode_chatter_owl.css") as app:
136
  gr.Markdown(
@@ -163,15 +190,17 @@ with gr.Blocks(css="light_mode_chatter_owl.css") as app:
163
 
164
  example_box = gr.Textbox(label="πŸ—£ Suggested Improvement", visible=True, placeholder="Click to generate improved speech...")
165
 
166
- # Session History Display
167
  def get_session_table():
168
  rows = [[s['timestamp'], s['transcript'][:60] + '...', s['feedback'][:60] + '...'] for s in session_history]
169
  return rows
170
 
171
  history_display = gr.Dataframe(headers=["πŸ•’ Timestamp", "πŸ“ Transcript", "πŸ“‹ Feedback Preview"], interactive=False)
172
  history_btn = gr.Button("πŸ•“ View My Past Sessions")
 
 
 
 
173
 
174
- # Connect functions
175
  audio_input.change(
176
  fn=tutor_feedback,
177
  inputs=[audio_input, language_dropdown],
@@ -196,7 +225,19 @@ with gr.Blocks(css="light_mode_chatter_owl.css") as app:
196
  outputs=history_display
197
  )
198
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # === Launch App ===
200
  if __name__ == "__main__":
201
  print("βœ… App is launching...")
202
- app.launch(server_name="0.0.0.0", server_port=7860, debug=True)
 
8
  import subprocess
9
  import shutil
10
  import datetime
11
+ import json
12
+ import csv
13
 
14
  # === Clean /tmp at startup ===
15
  TMP_DIR = "/tmp"
 
48
  model = WhisperModel("base", compute_type="int8")
49
  client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
50
 
51
+ # === Persistent session history ===
52
+ HISTORY_FILE = "history.json"
53
+ def load_history():
54
+ if os.path.exists(HISTORY_FILE):
55
+ with open(HISTORY_FILE, "r", encoding="utf-8") as f:
56
+ return json.load(f)
57
+ return []
58
+
59
+ def save_history(history):
60
+ with open(HISTORY_FILE, "w", encoding="utf-8") as f:
61
+ json.dump(history, f, ensure_ascii=False, indent=2)
62
+
63
+ session_history = load_history()
64
 
65
  # === Audio Processing ===
66
  def convert_to_wav(input_file):
 
134
  mp3_path = f"/tmp/{uuid.uuid4()}.mp3"
135
  tts.save(mp3_path)
136
 
 
137
  timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
138
+ session_entry = {
139
  "timestamp": timestamp,
140
  "transcript": transcript,
141
  "feedback": feedback_text
142
+ }
143
+ session_history.append(session_entry)
144
+ save_history(session_history)
145
 
146
  return transcript, feedback_text, mp3_path, transcript
147
 
148
+ # === Export history ===
149
+ def export_history_json():
150
+ return json.dumps(session_history, indent=2, ensure_ascii=False)
151
+
152
+ def export_history_csv():
153
+ csv_path = "/tmp/session_history.csv"
154
+ with open(csv_path, "w", newline='', encoding="utf-8") as f:
155
+ writer = csv.writer(f)
156
+ writer.writerow(["Timestamp", "Transcript", "Feedback"])
157
+ for s in session_history:
158
+ writer.writerow([s['timestamp'], s['transcript'], s['feedback']])
159
+ return csv_path
160
+
161
  # === Gradio Interface ===
162
  with gr.Blocks(css="light_mode_chatter_owl.css") as app:
163
  gr.Markdown(
 
190
 
191
  example_box = gr.Textbox(label="πŸ—£ Suggested Improvement", visible=True, placeholder="Click to generate improved speech...")
192
 
 
193
  def get_session_table():
194
  rows = [[s['timestamp'], s['transcript'][:60] + '...', s['feedback'][:60] + '...'] for s in session_history]
195
  return rows
196
 
197
  history_display = gr.Dataframe(headers=["πŸ•’ Timestamp", "πŸ“ Transcript", "πŸ“‹ Feedback Preview"], interactive=False)
198
  history_btn = gr.Button("πŸ•“ View My Past Sessions")
199
+ export_json_btn = gr.Button("πŸ“€ Export History (JSON)")
200
+ export_csv_btn = gr.Button("πŸ“„ Export History (CSV)")
201
+ export_json_output = gr.Textbox(visible=False)
202
+ export_csv_output = gr.File(label="Download CSV")
203
 
 
204
  audio_input.change(
205
  fn=tutor_feedback,
206
  inputs=[audio_input, language_dropdown],
 
225
  outputs=history_display
226
  )
227
 
228
+ export_json_btn.click(
229
+ fn=export_history_json,
230
+ inputs=None,
231
+ outputs=export_json_output
232
+ )
233
+
234
+ export_csv_btn.click(
235
+ fn=export_history_csv,
236
+ inputs=None,
237
+ outputs=export_csv_output
238
+ )
239
+
240
  # === Launch App ===
241
  if __name__ == "__main__":
242
  print("βœ… App is launching...")
243
+ app.launch(server_name="0.0.0.0", server_port=7860, debug=True)