adarsh commited on
Commit
e2f6fad
·
1 Parent(s): d87f7cd
Files changed (2) hide show
  1. app.py +104 -0
  2. requirements.txt +147 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+ from dotenv import load_dotenv
5
+ import logging
6
+
7
+ # Configure logging
8
+ logging.basicConfig(level=logging.INFO)
9
+ logger = logging.getLogger(__name__)
10
+
11
+ # Load environment variables
12
+ load_dotenv()
13
+
14
+ # dotenv_path = 'F:/backup-kali/codeFiles/projects/AgriAero/ai/server/.env'
15
+
16
+ # load_dotenv(dotenv_path)
17
+
18
+ class GeminiIntegration:
19
+ """Handles interaction with Google's Gemini API"""
20
+
21
+ def __init__(self, model_name: str = "gemini-2.0-flash", embedding_model: str = "text-embedding-004"):
22
+ self.model_name = model_name
23
+ self.embedding_model = embedding_model
24
+ self._configure_gemini()
25
+
26
+ def _configure_gemini(self):
27
+ """Configure Gemini API client"""
28
+ try:
29
+ api_key = os.getenv("GEMINI_API_KEY")
30
+ if not api_key:
31
+ raise ValueError("GEMINI_API_KEY not found in environment variables")
32
+ genai.configure(api_key=api_key)
33
+ logger.info("Gemini API configured successfully.")
34
+ except Exception as e:
35
+ logger.error(f"Failed to configure Gemini: {str(e)}")
36
+ raise
37
+
38
+ def generate_response(self, query: str, context: str = "") -> str:
39
+ """Generate a response from Gemini given a query and optional context"""
40
+ try:
41
+ system_prompt = """
42
+ You are an AI assistant for apple farmers. Respond only to queries related to apple cultivation, orchard management,
43
+ pest control, harvesting, and apple varieties. Keep your responses brief (80-100 words), use simple language that
44
+ farmers can easily understand, and focus on practical advice. If asked about topics unrelated to apple farming,
45
+ politely redirect to apple cultivation topics. You are part of the AgriAero Smart Apple Orchard Management System.
46
+ """
47
+
48
+ prompt = f"{system_prompt}\n\nContext: {context}\n\nQuestion: {query}"
49
+ response = genai.GenerativeModel(self.model_name).generate_content(prompt)
50
+ return response.text
51
+ except Exception as e:
52
+ logger.error(f"Response generation failed: {str(e)}")
53
+ return f"Sorry, I couldn't process your request. Please try again later."
54
+
55
+ # Initialize the Gemini integration
56
+ gemini = GeminiIntegration()
57
+
58
+ def process_query(query):
59
+ """Process user query and return response"""
60
+ if not query.strip():
61
+ return "Please enter a question about apple cultivation."
62
+
63
+ try:
64
+ response = gemini.generate_response(query)
65
+ return response
66
+ except Exception as e:
67
+ logger.error(f"Error processing query: {str(e)}")
68
+ return "Sorry, I'm having trouble connecting to the service. Please try again later."
69
+
70
+ # Create Gradio interface
71
+ with gr.Blocks(title="AgriAero Apple Cultivation Assistant") as app:
72
+ gr.Markdown("# 🍎 AgriAero Apple Cultivation Assistant")
73
+ gr.Markdown("Ask questions about apple cultivation and get simple, practical answers.")
74
+
75
+ with gr.Row():
76
+ with gr.Column():
77
+ query_input = gr.Textbox(
78
+ label="Your Question",
79
+ placeholder="Ask about apple varieties, pest control, pruning techniques, etc.",
80
+ lines=3
81
+ )
82
+ submit_btn = gr.Button("Get Answer", variant="primary")
83
+
84
+ response_output = gr.Textbox(label="Answer", lines=5)
85
+
86
+ # Examples
87
+ gr.Examples(
88
+ examples=[
89
+ "What are the best apple varieties for cold regions?",
90
+ "How do I control apple scab in my orchard?",
91
+ "When is the best time to prune apple trees?",
92
+ "How can I increase my apple yield?",
93
+ "What's the proper spacing for planting new apple trees?"
94
+ ],
95
+ inputs=query_input
96
+ )
97
+
98
+ # Set up event handlers
99
+ submit_btn.click(fn=process_query, inputs=query_input, outputs=response_output)
100
+ query_input.submit(fn=process_query, inputs=query_input, outputs=response_output)
101
+
102
+ # Launch app
103
+ if __name__ == "__main__":
104
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ aiohappyeyeballs==2.4.6
3
+ aiohttp==3.10.11
4
+ aiosignal==1.3.2
5
+ annotated-types==0.7.0
6
+ anyio==4.8.0
7
+ async-timeout==4.0.3
8
+ attrs==25.1.0
9
+ blinker==1.9.0
10
+ cachetools==5.5.2
11
+ certifi==2025.1.31
12
+ cffi==1.17.1
13
+ charset-normalizer==3.4.1
14
+ click==8.1.8
15
+ colorama==0.4.6
16
+ contourpy==1.3.1
17
+ cryptography==44.0.1
18
+ cycler==0.12.1
19
+ dataclasses-json==0.6.7
20
+ exceptiongroup==1.2.2
21
+ fastapi==0.115.8
22
+ ffmpy==0.5.0
23
+ filelock==3.17.0
24
+ Flask==3.1.0
25
+ fonttools==4.56.0
26
+ frozenlist==1.5.0
27
+ fsspec==2025.2.0
28
+ google-ai-generativelanguage==0.6.15
29
+ google-api-core==2.24.1
30
+ google-api-python-client==2.161.0
31
+ google-auth==2.38.0
32
+ google-auth-httplib2==0.2.0
33
+ google-generativeai==0.8.4
34
+ googleapis-common-protos==1.68.0
35
+ gradio==5.17.1
36
+ gradio_client==1.7.1
37
+ greenlet==3.1.1
38
+ grpcio==1.70.0
39
+ grpcio-status==1.70.0
40
+ h11==0.14.0
41
+ httpcore==1.0.7
42
+ httplib2==0.22.0
43
+ httpx==0.28.1
44
+ httpx-sse==0.4.0
45
+ huggingface-hub==0.29.1
46
+ idna==3.10
47
+ iniconfig==2.0.0
48
+ itsdangerous==2.2.0
49
+ Jinja2==3.1.5
50
+ joblib==1.4.2
51
+ jsonpatch==1.33
52
+ jsonpointer==3.0.0
53
+ jwt==1.3.1
54
+ kiwisolver==1.4.8
55
+ langchain==0.3.19
56
+ langchain-community==0.3.18
57
+ langchain-core==0.3.37
58
+ langchain-huggingface==0.1.2
59
+ langchain-pinecone==0.2.3
60
+ langchain-tests==0.3.12
61
+ langchain-text-splitters==0.3.6
62
+ langsmith==0.3.9
63
+ markdown-it-py==3.0.0
64
+ MarkupSafe==2.1.5
65
+ marshmallow==3.26.1
66
+ matplotlib==3.10.0
67
+ mdurl==0.1.2
68
+ mpmath==1.3.0
69
+ multidict==6.1.0
70
+ mypy-extensions==1.0.0
71
+ networkx==3.4.2
72
+ numpy==1.26.4
73
+ opencv-python==4.11.0.86
74
+ orjson==3.10.15
75
+ packaging==24.2
76
+ pandas==2.2.3
77
+ pillow==11.1.0
78
+ pinecone==5.4.2
79
+ pinecone-client==6.0.0
80
+ pinecone-plugin-inference==3.1.0
81
+ pinecone-plugin-interface==0.0.7
82
+ pluggy==1.5.0
83
+ propcache==0.3.0
84
+ proto-plus==1.26.0
85
+ protobuf==5.29.3
86
+ psutil==7.0.0
87
+ py-cpuinfo==9.0.0
88
+ pyasn1==0.6.1
89
+ pyasn1_modules==0.4.1
90
+ pycparser==2.22
91
+ pydantic==2.10.6
92
+ pydantic-settings==2.8.0
93
+ pydantic_core==2.27.2
94
+ pydub==0.25.1
95
+ Pygments==2.19.1
96
+ pyparsing==3.2.1
97
+ pytesseract==0.3.13
98
+ pytest==8.3.4
99
+ pytest-asyncio==0.25.3
100
+ pytest-socket==0.7.0
101
+ python-dateutil==2.9.0.post0
102
+ python-dotenv==1.0.1
103
+ python-multipart==0.0.20
104
+ pytz==2025.1
105
+ PyYAML==6.0.2
106
+ regex==2024.11.6
107
+ requests==2.32.3
108
+ requests-toolbelt==1.0.0
109
+ rich==13.9.4
110
+ rsa==4.9
111
+ ruff==0.9.7
112
+ safehttpx==0.1.6
113
+ safetensors==0.5.2
114
+ scikit-learn==1.6.1
115
+ scipy==1.15.2
116
+ seaborn==0.13.2
117
+ semantic-version==2.10.0
118
+ sentence-transformers==3.4.1
119
+ shellingham==1.5.4
120
+ six==1.17.0
121
+ sniffio==1.3.1
122
+ SQLAlchemy==2.0.38
123
+ starlette==0.45.3
124
+ sympy==1.13.1
125
+ syrupy==4.8.2
126
+ tenacity==9.0.0
127
+ threadpoolctl==3.5.0
128
+ tokenizers==0.21.0
129
+ tomli==2.2.1
130
+ tomlkit==0.13.2
131
+ torch==2.6.0
132
+ torchvision==0.21.0
133
+ tqdm==4.67.1
134
+ transformers==4.49.0
135
+ typer==0.15.1
136
+ typing-inspect==0.9.0
137
+ typing_extensions==4.12.2
138
+ tzdata==2025.1
139
+ ultralytics==8.3.78
140
+ ultralytics-thop==2.0.14
141
+ uritemplate==4.1.1
142
+ urllib3==2.3.0
143
+ uvicorn==0.34.0
144
+ websockets==14.2
145
+ Werkzeug==3.1.3
146
+ yarl==1.18.3
147
+ zstandard==0.23.0