Pontonkid commited on
Commit
cbc2e95
·
verified ·
1 Parent(s): 79dc6de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +144 -2
app.py CHANGED
@@ -7,10 +7,152 @@ import { generateWeatherForecast, getOutfitSuggestion, generateOutfitImage } fro
7
  import type { Weather, OutfitSuggestion } from './types';
8
 
9
  const App: React.FC = () => {
10
- arg1,
11
- arg2 )}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  </div>
13
  </div>
 
 
 
 
14
  </main>
15
  </div>
16
  );
 
7
  import type { Weather, OutfitSuggestion } from './types';
8
 
9
  const App: React.FC = () => {
10
+ const [location, setLocation] = useState<GeolocationCoordinates | null>(null);
11
+ const [weather, setWeather] = useState<Weather | null>(null);
12
+ const [wardrobe, setWardrobe] = useState<string>('- A pair of blue jeans\n- A white cotton t-shirt\n- Black denim jeans\n- A pair of sneakers\n- A warm wool sweater\n- Linen shorts\n- A waterproof rain jacket');
13
+ const [suggestion, setSuggestion] = useState<OutfitSuggestion | null>(null);
14
+ const [outfitImage, setOutfitImage] = useState<string | null>(null);
15
+
16
+ const [isLoading, setIsLoading] = useState(true);
17
+ const [isGenerating, setIsGenerating] = useState(false);
18
+ const [error, setError] = useState<string | null>(null);
19
+
20
+ const fetchWeather = useCallback(async (coords: GeolocationCoordinates) => {
21
+ setIsLoading(true);
22
+ setError(null);
23
+ try {
24
+ const weatherData = await generateWeatherForecast(coords.latitude, coords.longitude);
25
+ setWeather(weatherData);
26
+ } catch (err) {
27
+ setError('Could not fetch weather data. Please try again later.');
28
+ console.error(err);
29
+ } finally {
30
+ setIsLoading(false);
31
+ }
32
+ }, []);
33
+
34
+ useEffect(() => {
35
+ navigator.geolocation.getCurrentPosition(
36
+ (position) => {
37
+ setLocation(position.coords);
38
+ fetchWeather(position.coords);
39
+ },
40
+ (geoError) => {
41
+ setError('Geolocation is required. Please enable it in your browser settings.');
42
+ console.error(geoError);
43
+ setIsLoading(false);
44
+ }
45
+ );
46
+ // eslint-disable-next-line react-hooks/exhaustive-deps
47
+ }, [fetchWeather]);
48
+
49
+ const handleGenerateSuggestion = async () => {
50
+ if (!weather || !wardrobe.trim()) {
51
+ setError("Weather data and wardrobe inventory are required.");
52
+ return;
53
+ }
54
+ setIsGenerating(true);
55
+ setError(null);
56
+ setSuggestion(null);
57
+ setOutfitImage(null);
58
+
59
+ try {
60
+ const outfitSuggestion = await getOutfitSuggestion(weather, wardrobe);
61
+ setSuggestion(outfitSuggestion);
62
+
63
+ if (outfitSuggestion.outfit) {
64
+ const imageUrl = await generateOutfitImage(outfitSuggestion.outfit);
65
+ setOutfitImage(imageUrl);
66
+ }
67
+ } catch (err) {
68
+ setError('Failed to generate outfit suggestion. The model may be busy. Please try again.');
69
+ console.error(err);
70
+ } finally {
71
+ setIsGenerating(false);
72
+ }
73
+ };
74
+
75
+ return (
76
+ <div className="min-h-screen bg-gray-100 font-sans text-gray-800">
77
+ <main className="max-w-7xl mx-auto p-4 sm:p-6 lg:p-8">
78
+ <header className="text-center mb-10">
79
+ <h1 className="text-4xl md:text-5xl font-bold text-gray-800">ClothCast</h1>
80
+ <p className="text-lg text-gray-600 mt-2">Your AI-Powered Outfit Forecaster</p>
81
+ </header>
82
+
83
+ <div className="grid grid-cols-1 lg:grid-cols-2 gap-10 items-start">
84
+ {/* Left Column: Inputs */}
85
+ <div className="space-y-6 lg:sticky lg:top-8">
86
+ <Card
87
+ title="Your Wardrobe"
88
+ icon={<svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}><path strokeLinecap="round" strokeLinejoin="round" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" /></svg>}
89
+ >
90
+ <p className="text-sm text-gray-600 mb-4">List the clothes you have available. Be as descriptive as you like!</p>
91
+ <textarea
92
+ value={wardrobe}
93
+ onChange={(e) => setWardrobe(e.target.value)}
94
+ placeholder="e.g., Blue denim jacket, pair of white sneakers..."
95
+ className="w-full h-48 p-3 bg-gray-50 border border-gray-300 rounded-md focus:ring-2 focus:ring-orange-500 focus:border-orange-500 transition-shadow text-gray-700"
96
+ />
97
+ </Card>
98
+ <Button onClick={handleGenerateSuggestion} isLoading={isGenerating} disabled={isLoading || !wardrobe.trim()}>
99
+ Get Outfit Suggestion
100
+ </Button>
101
+ </div>
102
+
103
+ {/* Right Column: Outputs */}
104
+ <div className="space-y-8">
105
+ <Card
106
+ title="Today's Forecast"
107
+ icon={<svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}><path strokeLinecap="round" strokeLinejoin="round" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" /><path strokeLinecap="round" strokeLinejoin="round" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z" /></svg>}
108
+ >
109
+ {isLoading ? <Loader text="Fetching local weather..." /> :
110
+ error && !weather ? <p className="text-red-500">{error}</p> :
111
+ weather && (
112
+ <div className="flex items-center gap-6">
113
+ <WeatherIcon condition={weather.condition} className="w-20 h-20" />
114
+ <div>
115
+ <p className="text-3xl font-bold">{weather.temperature}°C in <span className="text-orange-600">{weather.location}</span></p>
116
+ <p className="text-gray-600">{weather.description}</p>
117
+ <p className="text-sm text-gray-500 mt-1">Humidity: {weather.humidity}%</p>
118
+ </div>
119
+ </div>
120
+ )}
121
+ </Card>
122
+
123
+ {(isGenerating || suggestion || outfitImage || (error && !isGenerating)) && (
124
+ <Card
125
+ title="AI Suggestion"
126
+ icon={<svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5 text-gray-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}><path strokeLinecap="round" strokeLinejoin="round" d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" /></svg>}
127
+ >
128
+ {isGenerating && <Loader text="Crafting your perfect outfit..." />}
129
+ {error && !isGenerating && <p className="text-red-500 text-center">{error}</p>}
130
+ {!isGenerating && suggestion && (
131
+ <div className="space-y-6">
132
+ {outfitImage ? (
133
+ <img src={outfitImage} alt="Generated outfit" className="rounded-lg shadow-md w-full object-cover aspect-[3/4]" />
134
+ ) : (
135
+ <div className="w-full aspect-[3/4] bg-gray-100 rounded-lg flex items-center justify-center animate-pulse">
136
+ <Loader text="Generating image..." />
137
+ </div>
138
+ )}
139
+ <p className="text-gray-700 leading-relaxed">{suggestion.outfit}</p>
140
+ {suggestion.laundry_alert && (
141
+ <div className="p-4 bg-amber-100 border-l-4 border-amber-500 text-amber-800 rounded-r-lg">
142
+ <p className="font-semibold">Laundry Alert!</p>
143
+ <p>{suggestion.laundry_alert}</p>
144
+ </div>
145
+ )}
146
+ </div>
147
+ )}
148
+ </Card>
149
+ )}
150
  </div>
151
  </div>
152
+
153
+ <footer className="text-center mt-12 text-sm text-gray-500">
154
+ <p>Powered by <a href="https://ai.google.dev/" target="_blank" rel="noopener noreferrer" className="font-semibold text-gray-600 hover:underline">Google Gemini</a></p>
155
+ </footer>
156
  </main>
157
  </div>
158
  );