home ramadies code

 import React, { useState } from 'react';


export default function App() {

    const [query, setQuery] = useState('');

    const [remedy, setRemedy] = useState('');

    const [isLoading, setIsLoading] = useState(false);

    const [error, setError] = useState('');


    // Function to call the Gemini API for remedy generation

    const generateRemedy = async (userQuery) => {

        setIsLoading(true);

        setRemedy('');

        setError('');


        // Define severe illness keywords

        const severeIllnessKeywords = [

            'cancer', 'heart disease', 'diabetes (long-term)', 'neurological disorders',

            'severe infection', 'chronic kidney disease', 'liver cirrhosis', 'autoimmune disease',

            'stroke', 'tuberculosis', 'HIV', 'malaria', 'dengue', 'long-term pain',

            'epilepsy', 'mental illness', 'schizophrenia', 'bipolar disorder', 'severe depression'

        ];


        // Check if the user query contains a severe illness keyword

        const isSevereIllness = severeIllnessKeywords.some(keyword =>

            userQuery.toLowerCase().includes(keyword)

        );


        let systemInstruction = `You are an AI assistant specializing in Ayurvedic home remedies. Provide advice based on traditional Ayurvedic principles.

If a user asks about a common plant, provide its known Ayurvedic uses and benefits.

If a user asks about an illness, provide simple, general home remedies involving plants or fruits if appropriate for mild conditions.

IMPORTANT: For severe or chronic illnesses, or conditions causing persistent, debilitating symptoms, you MUST strongly advise consulting a qualified medical doctor or an experienced Ayurvedic practitioner and state that this AI cannot replace professional medical advice. Always prioritize safety and professional consultation for serious health concerns.

Keep remedies general and do not recommend specific dosages or personalized treatments. Do not give any medical advice.

Always be polite and helpful.`;


        let prompt;

        if (isSevereIllness) {

            prompt = `User asked about "${userQuery}". This appears to be a severe or chronic illness. Please advise the user to consult a qualified medical doctor or an experienced Ayurvedic practitioner. Explicitly state that this AI cannot provide medical advice for such conditions.`;

        } else {

            prompt = `User query: "${userQuery}". Provide Ayurvedic home remedy information or plant uses.`;

        }


        try {

            let chatHistory = [];

            chatHistory.push({ role: "user", parts: [{ text: prompt }] });


            const payload = {

                contents: chatHistory,

                systemInstruction: { parts: [{ text: systemInstruction }] } // Using system instruction

            };


            // Canvas will provide the API key automatically if it's empty

            const apiKey = "";

            const apiUrl = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${apiKey}`;


            const response = await fetch(apiUrl, {

                method: 'POST',

                headers: { 'Content-Type': 'application/json' },

                body: JSON.stringify(payload)

            });


            const result = await response.json();


            if (result.candidates && result.candidates.length > 0 &&

                result.candidates[0].content && result.candidates[0].content.parts &&

                result.candidates[0].content.parts.length > 0) {

                const text = result.candidates[0].content.parts[0].text;

                setRemedy(text);

            } else {

                setError("Could not generate a response. Please try again.");

                console.error("Gemini API response structure unexpected:", result);

            }

        } catch (err) {

            setError("Failed to connect to the remedy service. Please check your network and try again.");

            console.error("Error fetching from Gemini API:", err);

        } finally {

            setIsLoading(false);

        }

    };


    const handleSubmit = (e) => {

        e.preventDefault();

        if (query.trim()) {

            generateRemedy(query);

        } else {

            setError("Please enter a plant name or an illness.");

        }

    };


    return (

        <div className="min-h-screen bg-gradient-to-br from-green-50 to-teal-100 flex flex-col items-center justify-center p-4 font-inter text-gray-800">

            <style>

                {`

                @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

                body { font-family: 'Inter', sans-serif; }

                `}

            </style>


            <div className="w-full max-w-2xl bg-white p-8 rounded-2xl shadow-xl border border-gray-200">

                <h1 className="text-4xl font-bold text-center text-green-700 mb-6 drop-shadow-sm">

                    Ayurvedic Home Remedies

                </h1>

                <p className="text-center text-gray-600 mb-8">

                    Enter a plant name to learn its Ayurvedic uses, or an illness for home remedy suggestions.

                    For serious conditions, always consult a doctor.

                </p>


                <form onSubmit={handleSubmit} className="space-y-6">

                    <div>

                        <label htmlFor="query" className="block text-lg font-medium text-gray-700 mb-2">

                            Enter Plant Name or Illness:

                        </label>

                        <textarea

                            id="query"

                            className="w-full p-4 border border-green-300 rounded-lg focus:ring-green-500 focus:border-green-500 shadow-sm transition-all duration-200 resize-y min-h-[100px]"

                            rows="4"

                            value={query}

                            onChange={(e) => setQuery(e.target.value)}

                            placeholder="e.g., Turmeric, Common Cold, Diabetes"

                            required

                        ></textarea>

                    </div>


                    <button

                        type="submit"

                        className="w-full bg-green-600 hover:bg-green-700 text-white font-semibold py-3 px-6 rounded-lg shadow-md transition transform hover:scale-105 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-opacity-75"

                        disabled={isLoading}

                    >

                        {isLoading ? (

                            <span className="flex items-center justify-center">

                                <svg className="animate-spin -ml-1 mr-3 h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">

                                    <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>

                                    <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>

                                </svg>

                                Generating...

                            </span>

                        ) : (

                            'Get Remedy'

                        )}

                    </button>

                </form>


                {error && (

                    <div className="mt-6 p-4 bg-red-100 text-red-700 rounded-lg border border-red-300">

                        <p className="font-medium">Error:</p>

                        <p>{error}</p>

                    </div>

                )}


                {remedy && (

                    <div className="mt-8 p-6 bg-green-50 border border-green-200 rounded-lg shadow-inner">

                        <h2 className="text-2xl font-semibold text-green-800 mb-4">Ayurvedic Insight:</h2>

                        <div className="prose max-w-none leading-relaxed text-gray-700">

                            {remedy.split('\n').map((paragraph, index) => (

                                <p key={index} className="mb-2 last:mb-0">{paragraph}</p>

                            ))}

                        </div>

                    </div>

                )}


                <div className="mt-10 text-center text-sm text-gray-500">

                    <p>This tool provides general Ayurvedic information and is not a substitute for professional medical advice.</p>

                    <p>Always consult a qualified healthcare practitioner for any health concerns.</p>

                </div>

            </div>

        </div>

    );

}


Comments

Popular posts from this blog

ch 2 pm

pm unit :1

ch 3 pm