-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
60 lines (54 loc) · 1.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React, { useState } from "react";
import axios from "axios";
const GEMINI_API_KEY = "Your-api-key"; // Replace with your actual API key
const GEMINI_API_URL = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${GEMINI_API_KEY}`;
const callGeminiAPI = async (lifestyle, foodhabits) => {
try {
const prompt = `Lifestyle: ${lifestyle}, Food Habits: ${foodhabits}`;
const response = await axios.post(
GEMINI_API_URL,
{
contents: [{ parts: [{ text: prompt }] }],
},
{
headers: { "Content-Type": "application/json" },
}
);
return response.data?.candidates?.[0]?.content?.parts?.[0]?.text || "No response received";;
} catch (error) {
console.error("Error calling Gemini API:", error);
return null;
}
};
const App = () => {
const [lifestyle, setLifestyle] = useState("");
const [foodhabits, setFoodhabits] = useState("");
const [responseText, setResponseText] = useState("");
const handleGenerateResponse = async () => {
if (!lifestyle || !foodhabits) return;
const result = await callGeminiAPI(lifestyle, foodhabits);
if (result) {
setResponseText(result);
}
};
return (
<div>
<h2>Gemini API Call</h2>
<input
type="text"
value={lifestyle}
onChange={(e) => setLifestyle(e.target.value)}
placeholder="Enter Lifestyle"
/>
<input
type="text"
value={foodhabits}
onChange={(e) => setFoodhabits(e.target.value)}
placeholder="Enter Food Habits"
/>
<button onClick={handleGenerateResponse}>Generate Response</button>
{responseText && <p>{responseText}</p>}
</div>
);
};
export default App;