nakas commited on
Commit
d6148dc
·
verified ·
1 Parent(s): 6cc6c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -106
app.py CHANGED
@@ -1,129 +1,112 @@
1
  import gradio as gr
2
- from playwright.sync_api import sync_playwright
3
  import pandas as pd
4
- import time
5
- import json
6
  from datetime import datetime
7
- import subprocess
8
- import sys
9
- import os
10
 
11
- def install_playwright_browsers():
12
- try:
13
- if not os.path.exists('/home/user/.cache/ms-playwright'):
14
- print("Installing Playwright browsers...")
15
- subprocess.run(
16
- [sys.executable, "-m", "playwright", "install", "chromium"],
17
- check=True,
18
- capture_output=True,
19
- text=True
20
- )
21
- print("Playwright browsers installed successfully")
22
- except Exception as e:
23
- print(f"Error installing browsers: {e}")
24
-
25
- install_playwright_browsers()
26
-
27
- def scrape_weather_data(site_id="YCTIM", hours=720):
28
- """Debug version that prints page content"""
29
- url = f"https://www.weather.gov/wrh/timeseries?site={site_id}&hours={hours}&units=english&chart=on&headers=on&obs=tabular&hourly=false&pview=full&font=12&plot="
30
 
31
- try:
32
- with sync_playwright() as p:
33
- browser = p.chromium.launch(
34
- headless=True,
35
- args=['--no-sandbox', '--disable-dev-shm-usage']
36
- )
37
- context = browser.new_context(
38
- user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
39
- )
40
- page = context.new_page()
41
-
42
- print("\nNavigating to URL...")
43
- response = page.goto(url)
44
- print(f"Response status: {response.status}")
45
-
46
- # Wait a bit and get the content
47
- time.sleep(5)
48
-
49
- # Print all HTML content
50
- content = page.content()
51
- print("\nPage Content:")
52
- print("=" * 80)
53
- print(content)
54
- print("=" * 80)
55
-
56
- # Print all text content
57
- text_content = page.evaluate('() => document.body.innerText')
58
- print("\nText Content:")
59
- print("=" * 80)
60
- print(text_content)
61
- print("=" * 80)
62
-
63
- # List all elements
64
- elements = page.evaluate('''() => {
65
- const all = document.getElementsByTagName('*');
66
- return Array.from(all).map(el => ({
67
- tag: el.tagName,
68
- id: el.id,
69
- class: el.className
70
- }));
71
- }''')
72
- print("\nPage Elements:")
73
- print("=" * 80)
74
- for el in elements:
75
- print(f"Tag: {el['tag']}, ID: {el['id']}, Class: {el['class']}")
76
- print("=" * 80)
77
 
78
- # Save screenshot
79
- page.screenshot(path="debug_screenshot.png")
80
- print("\nSaved screenshot as debug_screenshot.png")
81
 
82
- browser.close()
 
 
 
 
83
 
84
- return {
85
- 'status': 'Debug',
86
- 'message': 'Check console output for page content'
 
 
 
 
 
 
87
  }
88
-
89
- except Exception as e:
90
- return {
91
- 'status': 'Error',
92
- 'error_message': str(e)
93
- }
94
 
95
- def format_output(result):
96
- """Simple output formatter for debug version"""
97
- if result['status'] == 'Error':
98
- return f"Error: {result['error_message']}", None, None
99
- return result['message'], None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Create Gradio interface
102
- with gr.Blocks(title="Weather Data Scraper Debug") as demo:
103
- gr.Markdown("# Weather Data Scraper (Debug Version)")
 
104
 
105
  with gr.Row():
106
- site_id = gr.Textbox(
107
- label="Weather Station ID",
108
- value="YCTIM",
109
- placeholder="Enter station ID (e.g., YCTIM)"
110
- )
111
- hours = gr.Number(
112
- label="Hours of Data",
113
- value=720,
114
- minimum=1,
115
- maximum=1440
116
  )
117
 
118
- analyze_btn = gr.Button("Fetch and Print Page Content")
 
 
 
119
 
120
  with gr.Row():
121
- output = gr.HTML(label="Output")
 
122
 
123
  analyze_btn.click(
124
- fn=lambda sid, hrs: format_output(scrape_weather_data(sid, hrs)),
125
- inputs=[site_id, hours],
126
- outputs=[output]
127
  )
128
 
129
  if __name__ == "__main__":
 
1
  import gradio as gr
 
2
  import pandas as pd
3
+ import numpy as np
 
4
  from datetime import datetime
5
+ import re
 
 
6
 
7
+ def parse_weather_data(text):
8
+ """Parse the weather data text into a structured format"""
9
+ lines = [line.strip() for line in text.split('\n') if line.strip()]
10
+ data = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ for line in lines:
13
+ if not ('am' in line or 'pm' in line):
14
+ continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ parts = line.split('\t')
17
+ if len(parts) < 8:
18
+ continue
19
 
20
+ try:
21
+ # Parse wind speed and gusts (format: "12G30")
22
+ wind_match = re.search(r'(\d+)G(\d+)', parts[6]) if len(parts) > 6 else None
23
+ wind_speed = int(wind_match.group(1)) if wind_match else np.nan
24
+ wind_gust = int(wind_match.group(2)) if wind_match else np.nan
25
 
26
+ row = {
27
+ 'datetime': parts[0].strip(),
28
+ 'temperature': float(parts[1]),
29
+ 'dew_point': float(parts[2]),
30
+ 'humidity': float(parts[3]),
31
+ 'wind_chill': float(parts[4]) if parts[4].strip() != '' else np.nan,
32
+ 'wind_speed': wind_speed,
33
+ 'wind_gust': wind_gust,
34
+ 'snow_depth': float(parts[7]) if parts[7].strip() != '' else np.nan
35
  }
36
+ data.append(row)
37
+ except:
38
+ continue
39
+
40
+ return pd.DataFrame(data)
 
41
 
42
+ def analyze_weather_data(data_str):
43
+ """Analyze weather data and create visualizations"""
44
+ try:
45
+ # Parse data
46
+ df = parse_weather_data(data_str)
47
+ if len(df) == 0:
48
+ return "Error: No valid data found in input."
49
+
50
+ # Calculate statistics
51
+ stats = {
52
+ 'Temperature Range': f"{df['temperature'].min():.1f}°F to {df['temperature'].max():.1f}°F",
53
+ 'Average Temperature': f"{df['temperature'].mean():.1f}°F",
54
+ 'Max Wind Speed': f"{df['wind_speed'].max():.1f} mph",
55
+ 'Max Wind Gust': f"{df['wind_gust'].max():.1f} mph",
56
+ 'Average Humidity': f"{df['humidity'].mean():.1f}%",
57
+ 'Max Snow Depth': f"{df['snow_depth'].max():.1f} inches"
58
+ }
59
+
60
+ # Create HTML output
61
+ html_output = "<div style='font-size: 16px; line-height: 1.5;'>"
62
+ for key, value in stats.items():
63
+ html_output += f"<p><strong>{key}:</strong> {value}</p>"
64
+ html_output += "</div>"
65
+
66
+ # Create temperature plot
67
+ temp_fig = gr.Plot()
68
+ df.plot(x='datetime', y=['temperature', 'wind_chill'],
69
+ title='Temperature and Wind Chill Over Time',
70
+ figsize=(12, 6))
71
+ temp_fig.pyplot()
72
+
73
+ # Create wind plot
74
+ wind_fig = gr.Plot()
75
+ df.plot(x='datetime', y=['wind_speed', 'wind_gust'],
76
+ title='Wind Speed and Gusts Over Time',
77
+ figsize=(12, 6))
78
+ wind_fig.pyplot()
79
+
80
+ return html_output, temp_fig, wind_fig
81
+
82
+ except Exception as e:
83
+ return f"Error analyzing data: {str(e)}", None, None
84
 
85
  # Create Gradio interface
86
+ with gr.Blocks(title="Weather Data Analyzer") as demo:
87
+ gr.Markdown("# Weather Data Analyzer")
88
+ gr.Markdown("Paste weather data from weather.gov/wrh/timeseries in the text box below.")
89
 
90
  with gr.Row():
91
+ input_text = gr.Textbox(
92
+ label="Weather Data",
93
+ placeholder="Paste weather data here...",
94
+ lines=10
 
 
 
 
 
 
95
  )
96
 
97
+ analyze_btn = gr.Button("Analyze Weather Data")
98
+
99
+ with gr.Row():
100
+ stats_output = gr.HTML(label="Statistics")
101
 
102
  with gr.Row():
103
+ temp_plot = gr.Plot(label="Temperature Plot")
104
+ wind_plot = gr.Plot(label="Wind Plot")
105
 
106
  analyze_btn.click(
107
+ fn=analyze_weather_data,
108
+ inputs=[input_text],
109
+ outputs=[stats_output, temp_plot, wind_plot]
110
  )
111
 
112
  if __name__ == "__main__":