clementBE commited on
Commit
b8d5f44
·
verified ·
1 Parent(s): 58b3135

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -4,11 +4,25 @@ import plotly.express as px
4
  import os
5
 
6
  def process_file(file):
7
- # Load CSV and convert Unix timestamp to datetime
8
- df = pd.read_csv(file.name)
9
- df['date'] = pd.to_datetime(df['timestamp'], unit='s') # convert Unix timestamp
10
 
11
- # --- Plot 1: Messages over time ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  df['date_only'] = df['date'].dt.date
13
  messages_per_day = df.groupby("date_only").size().reset_index(name="count")
14
  fig1 = px.line(messages_per_day, x="date_only", y="count", title="Messages per Day")
@@ -19,21 +33,22 @@ def process_file(file):
19
  fig2 = px.bar(top_authors, x='author', y='count', title="Top 20 Authors", text='count')
20
  fig2.update_layout(xaxis_tickangle=-45)
21
 
22
- # (Optional) delete the uploaded file
23
  os.remove(file.name)
24
 
25
- return fig1, fig2
26
 
27
  # Gradio interface
28
  interface = gr.Interface(
29
  fn=process_file,
30
- inputs=gr.File(label="Upload your CSV file", file_types=[".csv"]),
31
  outputs=[
32
- gr.Plot(label="Messages by Day"),
33
- gr.Plot(label="Messages by Author"),
 
34
  ],
35
  title="Message Analyzer",
36
- description="Upload a CSV file with at least 'author' and 'timestamp' (Unix) columns."
37
  )
38
 
39
  if __name__ == "__main__":
 
4
  import os
5
 
6
  def process_file(file):
7
+ # Determine file extension
8
+ file_ext = os.path.splitext(file.name)[1].lower()
 
9
 
10
+ # Load file accordingly
11
+ if file_ext == '.csv':
12
+ df = pd.read_csv(file.name)
13
+ elif file_ext in ['.xls', '.xlsx']:
14
+ df = pd.read_excel(file.name)
15
+ else:
16
+ return "Unsupported file format", None, None
17
+
18
+ # Ensure 'timestamp' column exists
19
+ if 'timestamp' not in df.columns or 'author' not in df.columns:
20
+ return "The file must contain 'timestamp' and 'author' columns.", None, None
21
+
22
+ # Convert Unix timestamp to datetime
23
+ df['date'] = pd.to_datetime(df['timestamp'], unit='s')
24
+
25
+ # --- Plot 1: Messages per Day ---
26
  df['date_only'] = df['date'].dt.date
27
  messages_per_day = df.groupby("date_only").size().reset_index(name="count")
28
  fig1 = px.line(messages_per_day, x="date_only", y="count", title="Messages per Day")
 
33
  fig2 = px.bar(top_authors, x='author', y='count', title="Top 20 Authors", text='count')
34
  fig2.update_layout(xaxis_tickangle=-45)
35
 
36
+ # Optional: delete file after processing
37
  os.remove(file.name)
38
 
39
+ return "Success", fig1, fig2
40
 
41
  # Gradio interface
42
  interface = gr.Interface(
43
  fn=process_file,
44
+ inputs=gr.File(label="Upload CSV or XLSX", file_types=[".csv", ".xls", ".xlsx"]),
45
  outputs=[
46
+ gr.Textbox(label="Status"),
47
+ gr.Plot(label="Messages per Day"),
48
+ gr.Plot(label="Top Authors"),
49
  ],
50
  title="Message Analyzer",
51
+ description="Upload a CSV or XLSX file with 'timestamp' (Unix) and 'author' columns."
52
  )
53
 
54
  if __name__ == "__main__":