import google.generativeai as genai import gradio as gr import os # Get the API Key api_key = os.getenv("GOOGLE_API_KEY") genai.configure(api_key=api_key) # Initialize model model = genai.GenerativeModel("gemini-pro") # Define a prompt template prompt_template = """You are a virtual assistant designed to provide Kerala farmers with comprehensive information about Kerala agriculture. you know both malayalam and english ,only when then demand you shuould coomunicate both in malayalam otherwise use english , Your knowledge base includes: # What are the symptoms of Yellow Stem Borer on rice fields? The insect may start attacking the plants from the nursery stage. The incidence is high from October to January and February. The caterpillar enters the stem and feeds on the growing shoot. As a result the central shoot dries up and produces the characteristic dead heart. The dead tillers can be easily pulled from the base. When they attack at the time of flowering, the earheads become chaffy and and white in colour. Eggs seen in masses of 15- 80 covered with buff-coloured hairs on the upper leaf surface. Larva pale yellow with dark brown head. # What are the symptoms of Brown Plant attack on rice fields Hopper? Nymphs and adults congregate at the base of the plant above the water level and suck the sap from the tillers. The affected plant dries up and gives a scorched appearance called “hopper burn”. Circular patches of drying and lodging of matured plants are typical symptoms caused by this pest. It is the vector of grassy stunt, ragged stunt and wilted stunt diseases. White to brown nymphs and brown or white adults feeding near the base of tillers can be seen. # What are the symptoms of Rice Bug attack on fields? The attack of rice bug is seen in the milky stage. They suck the sap from grains. The attacked grains show brownish discoloured patches on the husk. Some grains or ear heads appear chaffy. The panicles will be seen standing erect because of low weight. Bugs can be seen flying about when disturbed. The bug also emits a particular buggy odour. Brownish green adults are slender with long legs and antennae. """ # Start the chat without the 'context' argument chat = model.start_chat(history=[]) # Send the prompt template as the first message in the chat chat.send_message(prompt_template) def get_gemini_response(question): response = chat.send_message(question, stream=True) full_response = "" for chunk in response: full_response += chunk.text return full_response # Interface using Gradio theme = gr.themes.Soft() with gr.Blocks(theme=theme, css=".gradio-container {background-color: #43fa4a}") as demo: gr.Markdown( """

Farm Extension GPT

Your Virtual Agricultural Assistant 🌱

""" ) chatbot = gr.Chatbot(elem_classes="colorful-chatbot") msg = gr.Textbox( label="Your Message", placeholder="Type your message here...", show_label=False, # Hide the label ) # Submit Button (Styled) submit_button = gr.Button( "Send", variant="primary", # Make it stand out ) # Clear Button (Styled) clear_button = gr.Button( "Clear Conversation", variant="secondary", # Less emphasis ) # Response Function (unchanged) def respond(message, chat_history): bot_message = get_gemini_response(message) chat_history.append((message, bot_message)) return "", chat_history # Submit Message & Update Chat submit_button.click(respond, [msg, chatbot], [msg, chatbot]) # Clear Chat History clear_button.click(lambda: None, None, chatbot, queue=False) # Launch the Chat Interface if __name__ == "__main__": demo.launch()