Pontonkid commited on
Commit
cfd876c
·
verified ·
1 Parent(s): db017d0

Create geminiservice.ts

Browse files
Files changed (1) hide show
  1. geminiservice.ts +115 -0
geminiservice.ts ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { GoogleGenAI, Type } from "@google/genai";
2
+ import type { Weather } from '../types';
3
+
4
+ // This function simulates fetching weather data using Gemini
5
+ // In a real app, this might be a call to a dedicated weather API
6
+ export async function generateWeatherForecast(lat: number, lon: number): Promise<Weather> {
7
+ const ai = new GoogleGenAI({ apiKey: process.env.API_KEY as string });
8
+ const prompt = `Based on the location (latitude: ${lat}, longitude: ${lon}), generate a realistic-looking weather forecast for today. Include a plausible city/area name for this location. Respond with ONLY a single, valid JSON object with the following structure: { "temperature": number, "condition": "Sunny" | "Cloudy" | "Rainy" | "Snowy" | "Windy" | "Stormy", "humidity": number, "description": string, "location": string }`;
9
+
10
+ try {
11
+ const response = await ai.models.generateContent({
12
+ model: 'gemini-2.5-flash',
13
+ contents: prompt,
14
+ config: {
15
+ responseMimeType: 'application/json',
16
+ responseSchema: {
17
+ type: Type.OBJECT,
18
+ properties: {
19
+ temperature: { type: Type.NUMBER },
20
+ condition: { type: Type.STRING },
21
+ humidity: { type: Type.NUMBER },
22
+ description: { type: Type.STRING },
23
+ location: { type: Type.STRING },
24
+ },
25
+ required: ['temperature', 'condition', 'humidity', 'description', 'location'],
26
+ },
27
+ }
28
+ });
29
+
30
+ const weatherData = JSON.parse(response.text);
31
+ // Basic validation
32
+ const validConditions = ['Sunny', 'Cloudy', 'Rainy', 'Snowy', 'Windy', 'Stormy'];
33
+ if (!validConditions.includes(weatherData.condition)) {
34
+ weatherData.condition = 'Cloudy'; // Default fallback
35
+ }
36
+
37
+ return weatherData as Weather;
38
+
39
+ } catch (error) {
40
+ console.error("Error generating weather forecast:", error);
41
+ // Return a default mock object on error
42
+ return {
43
+ temperature: 18,
44
+ condition: "Cloudy",
45
+ humidity: 65,
46
+ description: "Could not fetch weather. Using default values.",
47
+ location: "Unknown Location"
48
+ };
49
+ }
50
+ }
51
+
52
+
53
+ export async function getOutfitSuggestion(weather: Weather, wardrobe: string) {
54
+ const ai = new GoogleGenAI({ apiKey: process.env.API_KEY as string });
55
+
56
+ const prompt = `You are an expert fashion stylist named ClothCast. Your goal is to suggest an outfit based on the user's wardrobe and the local weather. Analyze the following weather conditions and wardrobe inventory.
57
+
58
+ Weather: ${JSON.stringify(weather)}
59
+
60
+ Wardrobe Inventory:
61
+ ---
62
+ ${wardrobe}
63
+ ---
64
+
65
+ Based on this, provide a practical and stylish outfit suggestion. Also, provide a 'laundry_alert' if the user seems to be running low on weather-appropriate clean clothes from their inventory.
66
+
67
+ Respond ONLY with a single, valid JSON object with the following structure:
68
+ { "outfit": "A descriptive suggestion of what to wear.", "laundry_alert": "A message about laundry, or null if not applicable." }
69
+
70
+ Example: { "outfit": "It's warm and humid. I suggest wearing your linen shorts, the white cotton t-shirt, and your comfortable sneakers. Avoid the black jeans as they'll be too hot.", "laundry_alert": "You've worn most of your shorts. It might be a good time to do laundry soon!" }`;
71
+
72
+ const response = await ai.models.generateContent({
73
+ model: 'gemini-2.5-pro',
74
+ contents: prompt,
75
+ config: {
76
+ responseMimeType: "application/json",
77
+ responseSchema: {
78
+ type: Type.OBJECT,
79
+ properties: {
80
+ outfit: {
81
+ type: Type.STRING,
82
+ description: "The outfit suggestion."
83
+ },
84
+ laundry_alert: {
85
+ type: Type.STRING,
86
+ description: "The laundry alert or null.",
87
+ nullable: true
88
+ }
89
+ },
90
+ required: ["outfit", "laundry_alert"]
91
+ }
92
+ }
93
+ });
94
+
95
+ return JSON.parse(response.text);
96
+ }
97
+
98
+
99
+ export async function generateOutfitImage(outfitDescription: string): Promise<string> {
100
+ const ai = new GoogleGenAI({ apiKey: process.env.API_KEY as string });
101
+ const prompt = `A photorealistic image of a person wearing this outfit: ${outfitDescription}. The style should be clean and minimalist, like a fashion catalogue, against a simple, neutral background. Show the full outfit. No text or logos.`;
102
+
103
+ const response = await ai.models.generateImages({
104
+ model: 'imagen-4.0-generate-001',
105
+ prompt,
106
+ config: {
107
+ numberOfImages: 1,
108
+ aspectRatio: '3:4',
109
+ outputMimeType: 'image/jpeg'
110
+ }
111
+ });
112
+
113
+ const base64ImageBytes = response.generatedImages[0].image.imageBytes;
114
+ return `data:image/jpeg;base64,${base64ImageBytes}`;
115
+ }