UmarBaba1 commited on
Commit
6997ae2
·
verified ·
1 Parent(s): 489ae30

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +86 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import requests
5
+ import io
6
+ import zipfile
7
+ from pydub import AudioSegment
8
+
9
+ def process_csv(file):
10
+ # Read CSV
11
+ df = pd.read_csv(file.name)
12
+
13
+ results = []
14
+ errors = []
15
+
16
+ # Process each audio link
17
+ for _, row in df.iterrows():
18
+ recorder_id = row.get("Fullname")
19
+ audio_url = row.get("Audio_Link")
20
+
21
+ if pd.isna(audio_url):
22
+ continue
23
+
24
+ try:
25
+ response = requests.get(audio_url, stream=True, timeout=30)
26
+ if response.status_code == 200:
27
+ audio_file = io.BytesIO(response.content)
28
+ audio = AudioSegment.from_file(audio_file)
29
+ duration_sec = len(audio) / 1000.0
30
+ duration_min = duration_sec / 60.0
31
+
32
+ results.append({
33
+ "recorder_id": recorder_id,
34
+ "audio_url": audio_url,
35
+ "duration_seconds": round(duration_sec, 2),
36
+ "duration_minutes": round(duration_min, 2)
37
+ })
38
+ else:
39
+ errors.append((recorder_id, audio_url, "Download failed"))
40
+ except Exception as e:
41
+ errors.append((recorder_id, audio_url, str(e)))
42
+
43
+ # Save results
44
+ output_df = pd.DataFrame(results)
45
+ output_filename = "audio_durations.csv"
46
+ output_df.to_csv(output_filename, index=False)
47
+
48
+ totals_df = output_df.groupby("recorder_id")[ "duration_minutes"].sum().reset_index()
49
+ totals_df.rename(columns={"duration_minutes": "total_minutes"}, inplace=True)
50
+ totals_filename = "total_durations_per_recorder.csv"
51
+ totals_df.to_csv(totals_filename, index=False)
52
+
53
+ error_filename = None
54
+ if errors:
55
+ error_df = pd.DataFrame(errors, columns=["recorder_id", "audio_url", "error"])
56
+ error_filename = "audio_errors.csv"
57
+ error_df.to_csv(error_filename, index=False)
58
+
59
+ # Create ZIP with results
60
+ zip_filename = "results.zip"
61
+ with zipfile.ZipFile(zip_filename, "w") as zipf:
62
+ zipf.write(output_filename)
63
+ zipf.write(totals_filename)
64
+ if error_filename:
65
+ zipf.write(error_filename)
66
+
67
+ return zip_filename
68
+
69
+ # Gradio UI
70
+ title = "Audio Duration Calculator"
71
+ description = """
72
+ Upload your CSV with 'Fullname' and 'Audio_Link' columns.
73
+ The app will calculate audio durations, total durations per recorder, and create error logs if needed.
74
+ You'll get a downloadable ZIP file.
75
+ """
76
+
77
+ demo = gr.Interface(
78
+ fn=process_csv,
79
+ inputs=gr.File(label="Upload CSV", type="file"),
80
+ outputs=gr.File(label="Download Results ZIP"),
81
+ title=title,
82
+ description=description
83
+ )
84
+
85
+ if __name__ == "__main__":
86
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+
2
+ gradio
3
+ pandas
4
+ pydub
5
+ requests
6
+ ffmpeg