Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,347 +1,70 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from
|
4 |
-
from
|
5 |
-
import
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
ZODIAC_SIGNS = [
|
18 |
-
("Aries", 0, 30),
|
19 |
-
("Taurus", 30, 60),
|
20 |
-
("Gemini", 60, 90),
|
21 |
-
("Cancer", 90, 120),
|
22 |
-
("Leo", 120, 150),
|
23 |
-
("Virgo", 150, 180),
|
24 |
-
("Libra", 180, 210),
|
25 |
-
("Scorpio", 210, 240),
|
26 |
-
("Sagittarius", 240, 270),
|
27 |
-
("Capricorn", 270, 300),
|
28 |
-
("Aquarius", 300, 330),
|
29 |
-
("Pisces", 330, 360),
|
30 |
-
]
|
31 |
-
|
32 |
-
# Moon phase boundaries (0° to 360° phase angle) for display purposes
|
33 |
-
MOON_PHASES = [
|
34 |
-
("New Moon", 0, 45),
|
35 |
-
("Waxing Crescent", 45, 90),
|
36 |
-
("First Quarter", 90, 135),
|
37 |
-
("Waxing Gibbous", 135, 180),
|
38 |
-
("Full Moon", 180, 225),
|
39 |
-
("Waning Gibbous", 225, 270),
|
40 |
-
("Last Quarter", 270, 315),
|
41 |
-
("Waning Crescent", 315, 360),
|
42 |
-
]
|
43 |
-
|
44 |
-
# Fertility sign coefficients (applicable to all plants)
|
45 |
-
FERTILITY_SIGN_COEFFS = {
|
46 |
-
"Aries": 1,
|
47 |
-
"Taurus": 2,
|
48 |
-
"Gemini": 0,
|
49 |
-
"Cancer": 2,
|
50 |
-
"Leo": 1,
|
51 |
-
"Virgo": 0,
|
52 |
-
"Libra": 0.5,
|
53 |
-
"Scorpio": 1.5,
|
54 |
-
"Sagittarius": 1,
|
55 |
-
"Capricorn": 1,
|
56 |
-
"Aquarius": 0,
|
57 |
-
"Pisces": 2,
|
58 |
-
}
|
59 |
-
|
60 |
-
# Pruning sign coefficients (applicable to all plants)
|
61 |
-
PRUNING_SIGN_COEFFS = {
|
62 |
-
"Aries": 1,
|
63 |
-
"Taurus": 0,
|
64 |
-
"Gemini": 2,
|
65 |
-
"Cancer": 0,
|
66 |
-
"Leo": 1,
|
67 |
-
"Virgo": 2,
|
68 |
-
"Libra": 1.5,
|
69 |
-
"Scorpio": 0.5,
|
70 |
-
"Sagittarius": 1,
|
71 |
-
"Capricorn": 1,
|
72 |
-
"Aquarius": 2,
|
73 |
-
"Pisces": 0,
|
74 |
-
}
|
75 |
-
|
76 |
-
# Fertility phase coefficients for above-ground plants
|
77 |
-
FERTILITY_PHASE_COEFFS_ABOVE = {
|
78 |
-
"New Moon": 0,
|
79 |
-
"Waxing Moon": 1,
|
80 |
-
"Full Moon": 0,
|
81 |
-
"Waning Moon": 0.5,
|
82 |
-
}
|
83 |
-
|
84 |
-
# Fertility phase coefficients for root crops
|
85 |
-
FERTILITY_PHASE_COEFFS_ROOT = {
|
86 |
-
"New Moon": 0,
|
87 |
-
"Waxing Moon": 0.5,
|
88 |
-
"Full Moon": 0,
|
89 |
-
"Waning Moon": 1,
|
90 |
-
}
|
91 |
-
|
92 |
-
# Pruning phase coefficients
|
93 |
-
PRUNING_PHASE_COEFFS = {
|
94 |
-
"New Moon": 0,
|
95 |
-
"Waxing Moon": 1,
|
96 |
-
"Full Moon": 0,
|
97 |
-
"Waning Moon": 0.5,
|
98 |
-
}
|
99 |
-
|
100 |
-
# Tool definitions
|
101 |
-
|
102 |
-
@tool
|
103 |
-
def get_moon_info(date_time: str) -> dict:
|
104 |
-
"""
|
105 |
-
Returns Moon's Zodiac position, phase, and fertility and pruning indices for the given date/time.
|
106 |
-
|
107 |
-
Args:
|
108 |
-
date_time (str): ISO 8601 formatted datetime (YYYY-MM-DDTHH:MM:SS)
|
109 |
-
Returns:
|
110 |
-
dict: {
|
111 |
-
"zodiac_position": "Leo 15°30'",
|
112 |
-
"moon_phase": "Waxing Gibbous",
|
113 |
-
"fertility_above_ground": 2.0,
|
114 |
-
"fertility_root_crop": 1.5,
|
115 |
-
"pruning": 2.0
|
116 |
-
}
|
117 |
-
"""
|
118 |
-
try:
|
119 |
-
# Parse input datetime and localize to UTC
|
120 |
-
user_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S")
|
121 |
-
user_time = pytz.utc.localize(user_time)
|
122 |
-
|
123 |
-
# Use loaded ephemeris and timescale
|
124 |
-
t = ts.from_datetime(user_time)
|
125 |
-
|
126 |
-
# Define celestial bodies
|
127 |
-
earth = planets['earth']
|
128 |
-
moon = planets['moon']
|
129 |
-
sun = planets['sun']
|
130 |
-
|
131 |
-
# Calculate Moon's ecliptic longitude
|
132 |
-
astrometric = earth.at(t).observe(moon)
|
133 |
-
ecliptic_lat, ecliptic_lon, distance = astrometric.ecliptic_latlon()
|
134 |
-
lon_deg = ecliptic_lon.degrees % 360
|
135 |
-
|
136 |
-
# Calculate the phase angle using almanac.moon_phase
|
137 |
-
phase = almanac.moon_phase(planets, t)
|
138 |
-
phase_angle = phase.degrees
|
139 |
-
|
140 |
-
# Determine Zodiac sign and position
|
141 |
-
zodiac_sign = "Unknown"
|
142 |
-
position_degrees = 0
|
143 |
-
for sign, start, end in ZODIAC_SIGNS:
|
144 |
-
if start <= lon_deg < end:
|
145 |
-
zodiac_sign = sign
|
146 |
-
position_degrees = lon_deg - start
|
147 |
-
break
|
148 |
-
|
149 |
-
# Format position to degrees and minutes
|
150 |
-
degrees = int(position_degrees)
|
151 |
-
minutes = int((position_degrees % 1) * 60)
|
152 |
-
position_str = f"{zodiac_sign} {degrees}°{minutes:02}'"
|
153 |
-
|
154 |
-
# Determine moon phase for display
|
155 |
-
moon_phase = "Unknown"
|
156 |
-
for phase, start, end in MOON_PHASES:
|
157 |
-
if start <= phase_angle < end:
|
158 |
-
moon_phase = phase
|
159 |
-
break
|
160 |
-
|
161 |
-
# Determine phase category for indices with 15° orbis for New and Full Moon
|
162 |
-
if (phase_angle >= 345 or phase_angle < 15):
|
163 |
-
phase_category = "New Moon" # 345° to 15° (30° total orbis)
|
164 |
-
elif 15 <= phase_angle < 165:
|
165 |
-
phase_category = "Waxing Moon"
|
166 |
-
elif 165 <= phase_angle < 195:
|
167 |
-
phase_category = "Full Moon" # 165° to 195° (30° total orbis)
|
168 |
-
elif 195 <= phase_angle < 345:
|
169 |
-
phase_category = "Waning Moon"
|
170 |
-
else:
|
171 |
-
phase_category = "Unknown"
|
172 |
-
|
173 |
-
# Calculate fertility and pruning indices
|
174 |
-
if zodiac_sign in FERTILITY_SIGN_COEFFS and phase_category in FERTILITY_PHASE_COEFFS_ABOVE:
|
175 |
-
fertility_above_ground = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ABOVE[phase_category]
|
176 |
-
fertility_root_crop = FERTILITY_SIGN_COEFFS[zodiac_sign] + FERTILITY_PHASE_COEFFS_ROOT[phase_category]
|
177 |
-
pruning = PRUNING_SIGN_COEFFS[zodiac_sign] + PRUNING_PHASE_COEFFS[phase_category]
|
178 |
-
else:
|
179 |
-
fertility_above_ground = None
|
180 |
-
fertility_root_crop = None
|
181 |
-
pruning = None
|
182 |
-
|
183 |
-
return {
|
184 |
-
"zodiac_position": position_str,
|
185 |
-
"moon_phase": moon_phase,
|
186 |
-
"fertility_above_ground": fertility_above_ground,
|
187 |
-
"fertility_root_crop": fertility_root_crop,
|
188 |
-
"pruning": pruning
|
189 |
-
}
|
190 |
-
|
191 |
-
except Exception as e:
|
192 |
-
raise ValueError(f"Error in get_moon_info: {str(e)}")
|
193 |
-
|
194 |
-
@tool
|
195 |
-
def get_current_time_in_timezone(timezone: str) -> str:
|
196 |
-
"""
|
197 |
-
Returns the current local time in the specified timezone with description.
|
198 |
-
|
199 |
-
Args:
|
200 |
-
timezone (str): A string representing a valid timezone (e.g., 'UTC')
|
201 |
-
Returns:
|
202 |
-
str: Formatted local time with timezone description
|
203 |
-
"""
|
204 |
-
try:
|
205 |
-
tz = pytz.timezone(timezone)
|
206 |
-
now = datetime.datetime.now(tz)
|
207 |
-
return f"Local time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
|
208 |
-
except Exception as e:
|
209 |
-
return f"Error: {str(e)}"
|
210 |
-
|
211 |
-
@tool
|
212 |
-
def get_current_time_raw(timezone: str) -> str:
|
213 |
-
"""
|
214 |
-
Returns current local time in specified timezone as ISO 8601 string.
|
215 |
-
|
216 |
-
Args:
|
217 |
-
timezone (str): A string representing a valid timezone (e.g., 'UTC')
|
218 |
-
Returns:
|
219 |
-
str: Datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS)
|
220 |
-
"""
|
221 |
-
try:
|
222 |
-
tz = pytz.timezone(timezone)
|
223 |
-
now = datetime.datetime.now(tz)
|
224 |
-
return now.strftime("%Y-%m-%dT%H:%M:%S")
|
225 |
-
except Exception as e:
|
226 |
-
return f"Error: {str(e)}"
|
227 |
-
|
228 |
-
# Memory initialization for state management
|
229 |
-
memory = SimpleMemory(
|
230 |
-
memory={
|
231 |
-
"location_provided": False,
|
232 |
-
"plant": None,
|
233 |
-
"root_crop": None,
|
234 |
-
"location_cautions": "",
|
235 |
-
"answer": "",
|
236 |
-
"last_question": None
|
237 |
-
}
|
238 |
)
|
239 |
|
240 |
-
#
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
Instructions:
|
254 |
-
1. If responding to a clarification question (last_question is not None), interpret the input as the answer to that question and update the state accordingly.
|
255 |
-
2. Otherwise, process the user's request as follows:
|
256 |
-
- Check if a plant name is provided and recognized. If not, ask "Please specify the plant you are interested in." If recognized, determine if it’s a root crop or above-ground plant (e.g., known plants: potato=root, tomato=above-ground). If unrecognized, ask "Is this plant a root crop? (yes/no)".
|
257 |
-
- Check if a location is provided. If yes, set location_provided to true. If the location is not on Earth (e.g., "Moon", "Mars"), set location_cautions to "Salute you explorer! Moon indices are Earth-specific due to gravitational and tidal influences. For other planets, develop indices based on local celestial cycles." and use it as the final answer. If on Earth, ask "Is this location suitable for outdoor planting? (yes/no)" to determine suitability.
|
258 |
-
- Determine if the request is about planting or pruning. For planting, ensure plant is defined (ask if not), then calculate the fertility index using get_moon_info. For pruning, calculate the pruning index.
|
259 |
-
- If location_cautions is not empty, append it to the answer.
|
260 |
-
3. When asking a question, format your response as:
|
261 |
-
"Action: Ask user\nQuestion: [your question]"
|
262 |
-
4. When all information is gathered, calculate the answer and call FinalAnswerTool.
|
263 |
-
"""
|
264 |
-
}
|
265 |
|
266 |
-
#
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
)
|
274 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
275 |
|
276 |
-
# Initialize the agent without the 'memory' parameter
|
277 |
-
agent = CodeAgent(
|
278 |
-
model=model,
|
279 |
-
tools=[final_answer, get_moon_info, get_current_time_in_timezone, get_current_time_raw],
|
280 |
-
max_steps=10,
|
281 |
-
verbosity_level=1,
|
282 |
-
prompt_templates=prompt_templates,
|
283 |
-
)
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
global memory, agent
|
288 |
-
if memory["last_question"] is not None:
|
289 |
-
if memory["last_question"] == "plant":
|
290 |
-
memory["plant"] = user_input
|
291 |
-
known_plants = {"potato": True, "tomato": False}
|
292 |
-
if user_input in known_plants:
|
293 |
-
memory["root_crop"] = known_plants[user_input]
|
294 |
-
else:
|
295 |
-
memory["last_question"] = "root_crop"
|
296 |
-
return "Action: Ask user\nQuestion: Is this plant a root crop? (yes/no)"
|
297 |
-
elif memory["last_question"] == "root_crop":
|
298 |
-
memory["root_crop"] = user_input.lower() in ["yes", "y"]
|
299 |
-
memory["last_question"] = None
|
300 |
-
elif memory["last_question"] == "location_suitability":
|
301 |
-
if user_input.lower() in ["no", "n"]:
|
302 |
-
memory["location_cautions"] = "Ensure required conditions for the plant (e.g., indoor) before relying on the fertility indices."
|
303 |
-
else:
|
304 |
-
memory["location_cautions"] = ""
|
305 |
-
memory["last_question"] = None
|
306 |
|
307 |
-
|
308 |
-
|
309 |
-
memory=memory.memory, # Pass the memory dictionary directly
|
310 |
-
input=user_input
|
311 |
-
)
|
312 |
-
|
313 |
-
# Run the agent with the updated prompt
|
314 |
-
output = agent.run(current_prompt)
|
315 |
-
|
316 |
-
if "Action: Ask user" in output:
|
317 |
-
question = output.split("Question: ")[1].strip()
|
318 |
-
if "plant" in question.lower():
|
319 |
-
memory["last_question"] = "plant"
|
320 |
-
elif "root crop" in question.lower():
|
321 |
-
memory["last_question"] = "root_crop"
|
322 |
-
elif "suitable for outdoor" in question.lower():
|
323 |
-
memory["last_question"] = "location_suitability"
|
324 |
-
memory["location_provided"] = True
|
325 |
-
return question
|
326 |
-
else:
|
327 |
-
if "Salute you explorer!" in output:
|
328 |
-
memory["location_cautions"] = output
|
329 |
-
memory["answer"] = output
|
330 |
-
elif memory["location_cautions"]:
|
331 |
-
memory["answer"] = output + " " + memory["location_cautions"]
|
332 |
-
else:
|
333 |
-
memory["answer"] = output
|
334 |
-
return output
|
335 |
|
336 |
-
|
337 |
-
interface = Interface(
|
338 |
-
fn=conversation_handler,
|
339 |
-
inputs="text",
|
340 |
-
outputs="text",
|
341 |
-
title="Garden Magus",
|
342 |
-
description="Ask about planting or pruning based on moon indices."
|
343 |
-
)
|
344 |
|
345 |
-
|
346 |
-
|
347 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
from llama_index.core import Document
|
3 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
4 |
+
from llama_index.core.node_parser import SentenceSplitter
|
5 |
+
from llama_index.core.ingestion import IngestionPipeline
|
6 |
+
from llama_index.core import SimpleDirectoryReader
|
7 |
+
|
8 |
+
reader = SimpleDirectoryReader(input_dir=r"C:\Users\so7\AppData\Local\Programs\Python\Python313\RAG")
|
9 |
+
documents = reader.load_data()
|
10 |
+
|
11 |
+
# create the pipeline with transformations
|
12 |
+
pipeline = IngestionPipeline(
|
13 |
+
transformations=[
|
14 |
+
SentenceSplitter(chunk_overlap=0),
|
15 |
+
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
|
16 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
)
|
18 |
|
19 |
+
# Define an async function to handle the pipeline
|
20 |
+
async def main():
|
21 |
+
# Create the pipeline with transformations
|
22 |
+
pipeline = IngestionPipeline(
|
23 |
+
transformations=[
|
24 |
+
SentenceSplitter(chunk_overlap=0),
|
25 |
+
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
|
26 |
+
]
|
27 |
+
)
|
28 |
+
# Use await inside the async function
|
29 |
+
nodes = await pipeline.arun(documents=[Document.example()])
|
30 |
+
# Optional: Do something with the nodes (e.g., print them)
|
31 |
+
print(nodes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Run the async function using asyncio
|
34 |
+
if __name__ == "__main__":
|
35 |
+
asyncio.run(main())
|
36 |
+
|
37 |
+
import chromadb
|
38 |
+
from llama_index.vector_stores.chroma import ChromaVectorStore
|
39 |
+
from llama_index.core.ingestion import IngestionPipeline
|
40 |
+
from llama_index.core.node_parser import SentenceSplitter
|
41 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
42 |
+
|
43 |
+
db = chromadb.PersistentClient(path="./pl_db")
|
44 |
+
chroma_collection = db.get_or_create_collection("ppgpl")
|
45 |
+
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
|
46 |
+
|
47 |
+
pipeline = IngestionPipeline(
|
48 |
+
transformations=[
|
49 |
+
SentenceSplitter(chunk_size=25, chunk_overlap=0),
|
50 |
+
HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5"),
|
51 |
+
],
|
52 |
+
vector_store=vector_store,
|
53 |
)
|
|
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
from llama_index.core import VectorStoreIndex
|
57 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
60 |
+
index = VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
llm = HuggingFaceInferenceAPI(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
|
65 |
+
query_engine = index.as_query_engine(
|
66 |
+
llm=llm,
|
67 |
+
response_mode="tree_summarize",
|
68 |
+
)
|
69 |
+
query_engine.query("Солнце на третей ступени")
|
70 |
+
# The meaning of life is 42
|