NitinBot001 commited on
Commit
f3f9d46
·
verified ·
1 Parent(s): b43b541

Upload 7 files

Browse files
Files changed (7) hide show
  1. .gitignore +174 -0
  2. Dockerfile +22 -0
  3. app.py +91 -0
  4. requirements.txt +5 -0
  5. static/context.txt +0 -0
  6. templates/index.html +78 -0
  7. vercel.json +15 -0
.gitignore ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+
110
+ # pdm
111
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
112
+ #pdm.lock
113
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
114
+ # in version control.
115
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
116
+ .pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
121
+ __pypackages__/
122
+
123
+ # Celery stuff
124
+ celerybeat-schedule
125
+ celerybeat.pid
126
+
127
+ # SageMath parsed files
128
+ *.sage.py
129
+
130
+ # Environments
131
+ .env
132
+ .venv
133
+ env/
134
+ venv/
135
+ ENV/
136
+ env.bak/
137
+ venv.bak/
138
+
139
+ # Spyder project settings
140
+ .spyderproject
141
+ .spyproject
142
+
143
+ # Rope project settings
144
+ .ropeproject
145
+
146
+ # mkdocs documentation
147
+ /site
148
+
149
+ # mypy
150
+ .mypy_cache/
151
+ .dmypy.json
152
+ dmypy.json
153
+
154
+ # Pyre type checker
155
+ .pyre/
156
+
157
+ # pytype static type analyzer
158
+ .pytype/
159
+
160
+ # Cython debug symbols
161
+ cython_debug/
162
+
163
+ # PyCharm
164
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
165
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
167
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
168
+ #.idea/
169
+
170
+ # Ruff stuff:
171
+ .ruff_cache/
172
+
173
+ # PyPI configuration file
174
+ .pypirc
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as a parent image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the current directory contents into the container at /app
8
+ COPY . /app
9
+
10
+ # Install any needed packages specified in requirements.txt
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Make port 8000 available to the world outside this container
14
+ EXPOSE 7860
15
+
16
+ # Define environment variable for Flask app and set Flask environment to development
17
+ ENV FLASK_APP=app.py
18
+ ENV FLASK_DEBUG=1
19
+ ENV FLASK_ENV=development
20
+
21
+ # Run app.py when the container launches
22
+ CMD ["flask", "run", "--host=0.0.0.0", "--port=7860"]
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, render_template
2
+ import os
3
+ from flask_cors import CORS
4
+ from googletrans import Translator
5
+ from openai import *
6
+
7
+ # Load API key and base URL from environment variables
8
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") or os.getenv("GEMINI_API_KEY")
9
+ if not OPENAI_API_KEY:
10
+ raise ValueError("OpenAI API Key is missing. Set it in environment variables.")
11
+
12
+ OPENAI_API_BASE = os.getenv("OPENAI_API_BASE", "https://generativelanguage.googleapis.com/v1beta/openai/") # Default is standard OpenAI
13
+ OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gemini-2.5-flash") # Default model
14
+
15
+ # Configure OpenAI client (supports custom base url for OpenAI-compatible APIs)
16
+ client = OpenAI(api_key=OPENAI_API_KEY, base_url=OPENAI_API_BASE)
17
+
18
+ # Set the static folder path to the "static" folder
19
+ STATIC_FOLDER = os.path.join(os.path.dirname(__file__), "static")
20
+
21
+ # Read the context file from the static folder
22
+ CONTEXT_FILE = os.path.join(STATIC_FOLDER, "context.txt")
23
+ try:
24
+ with open(CONTEXT_FILE, "r", encoding="utf-8") as file:
25
+ CONTEXT_DATA = file.read()
26
+ except FileNotFoundError:
27
+ CONTEXT_DATA = "No context available."
28
+
29
+ SYSTEM_INSTRUCTION = """
30
+ You are Hal, an AI assistant created to help farmers.
31
+ Your goal is to analyze the crop data provided in the context file and assist farmers by answering their queries and solving their problems.
32
+
33
+ Response Rules:
34
+ 1. Use the context file to answer farmer-related questions.
35
+ 2. Do not share any personal information.
36
+ 3. Provide only the information available in the context file.
37
+ 4. If not found, generate helpful answers from agricultural knowledge.
38
+ 5. Keep responses simple, clear, and useful.
39
+ """
40
+
41
+ # Flask app
42
+ app = Flask(__name__, template_folder="templates", static_folder="static")
43
+ CORS(app)
44
+
45
+ # Translator instance
46
+ translator = Translator()
47
+
48
+ def translate_text(text, target_lang):
49
+ try:
50
+ translated = translator.translate(text, dest=target_lang)
51
+ return translated.text
52
+ except Exception as e:
53
+ return f"Translation error: {str(e)}"
54
+
55
+ @app.route("/", methods=["GET"])
56
+ def home():
57
+ return render_template("index.html")
58
+
59
+ @app.route("/chat", methods=["POST"])
60
+ def chat():
61
+ try:
62
+ data = request.get_json()
63
+ user_message = data.get("message")
64
+ target_lang = data.get("lang", "en")
65
+
66
+ if not user_message:
67
+ return jsonify({"error": "Message is required"}), 400
68
+
69
+ messages = [
70
+ {"role": "system", "content": SYSTEM_INSTRUCTION},
71
+ {"role": "user", "content": f"Context Data:\n{CONTEXT_DATA}\n\nUser Query: {user_message}"}
72
+ ]
73
+
74
+ # Call OpenAI (or OpenAI-compatible) chat API
75
+ response = client.chat.completions.create(
76
+ model=OPENAI_MODEL,
77
+ messages=messages,
78
+ temperature=0.7
79
+ )
80
+ ai_response = response.choices[0].message.content.strip()
81
+
82
+ if target_lang.lower() != "en":
83
+ ai_response = translate_text(ai_response, target_lang)
84
+
85
+ return jsonify({"response": ai_response})
86
+ except Exception as e:
87
+ return jsonify({"error": str(e)}), 500
88
+
89
+
90
+ if __name__ == '__main__':
91
+ app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ Flask
2
+ googletrans==4.0.0-rc1
3
+ flask-cors
4
+ openai
5
+ requests
static/context.txt ADDED
The diff for this file is too large to render. See raw diff
 
templates/index.html ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>Hal Chat - Farmer Assistant (Milets)</title>
6
+ <style>
7
+ body {
8
+ font-family: Arial, sans-serif;
9
+ padding: 2rem;
10
+ background-color: #f4f4f4;
11
+ }
12
+ h1 {
13
+ color: #2c3e50;
14
+ }
15
+ label, select, input, textarea, button {
16
+ display: block;
17
+ margin-top: 1rem;
18
+ width: 100%;
19
+ }
20
+ textarea {
21
+ height: 100px;
22
+ }
23
+ #response {
24
+ margin-top: 2rem;
25
+ background: #fff;
26
+ padding: 1rem;
27
+ border-radius: 5px;
28
+ }
29
+ </style>
30
+ </head>
31
+ <body>
32
+ <h1>Ask Hal (Farmer AI)</h1>
33
+ <form id="chatForm">
34
+ <label for="message">Your Question:</label>
35
+ <textarea id="message" required></textarea>
36
+
37
+ <label for="lang">Response Language:</label>
38
+ <select id="lang">
39
+ <option value="en">English</option>
40
+ <option value="hi">Hindi</option>
41
+ <option value="mr">Marathi</option>
42
+ <option value="te">Telugu</option>
43
+ <option value="bn">Bengali</option>
44
+ </select>
45
+
46
+ <button type="submit">Ask</button>
47
+ </form>
48
+
49
+ <div id="response"></div>
50
+
51
+ <script>
52
+ document.getElementById("chatForm").addEventListener("submit", async (e) => {
53
+ e.preventDefault();
54
+
55
+ const message = document.getElementById("message").value;
56
+ const lang = document.getElementById("lang").value;
57
+
58
+ const responseBox = document.getElementById("response");
59
+ responseBox.innerHTML = "Thinking...";
60
+
61
+ try {
62
+ const res = await fetch("/chat", {
63
+ method: "POST",
64
+ headers: {
65
+ "Content-Type": "application/json"
66
+ },
67
+ body: JSON.stringify({ message, lang })
68
+ });
69
+
70
+ const data = await res.json();
71
+ responseBox.innerHTML = `<strong>Response:</strong><br>${data.response || data.error}`;
72
+ } catch (err) {
73
+ responseBox.innerHTML = "Error contacting the server.";
74
+ }
75
+ });
76
+ </script>
77
+ </body>
78
+ </html>
vercel.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "version": 2,
3
+ "builds": [
4
+ {
5
+ "src": "./app.py",
6
+ "use": "@vercel/python"
7
+ }
8
+ ],
9
+ "routes": [
10
+ {
11
+ "src": "/(.*)",
12
+ "dest": "/app.py"
13
+ }
14
+ ]
15
+ }