Download agents/CancerAgent.py from sp103107/skatingP: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/sp103107/skatingP/resolve/main/agents/CancerAgent.py
- Command line
-
hf download hf://spaces/sp103107/skatingP/agents/CancerAgent.py
-
curl -L -o CancerAgent.py https://huggingface.co/spaces/sp103107/skatingP/resolve/main/agents/CancerAgent.py
3.24 kB
| import logging | |
| import openai | |
| import os | |
| from dotenv import load_dotenv # Importing to load environment variables | |
| from swarm import Agent, Swarm | |
| # Load environment variables from .env file | |
| load_dotenv() # Load environment variables from the .env file | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| # Load OpenAI API key | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # Check if the API key was loaded successfully | |
| if not openai.api_key: | |
| raise ValueError("OpenAI API key is missing from the environment.") | |
| def generate_cancer_prediction(): | |
| swarm = Swarm() # Correct way to create a Swarm instance | |
| try: | |
| # Fetch data from agents | |
| planetary_data = swarm.call("NASAAgent") | |
| moon_phase_data = swarm.call("MoonAgent") | |
| tide_data = swarm.call("TidesAgent") | |
| # Extract relevant information | |
| current_planetary_title = planetary_data.get('title', 'Unknown planetary influence') | |
| current_moon_phase = moon_phase_data.get('moon_phase', 'Unknown phase') | |
| current_tide_level = tide_data[0].get('v', 'N/A') if isinstance(tide_data, list) else "N/A" | |
| # Build the prompt for OpenAI | |
| prompt = ( | |
| f"Generate a detailed horoscope report for Cancer using the following data:\n" | |
| f"- Current Moon phase: {current_moon_phase}\n" | |
| f"- Planetary alignment: {current_planetary_title}\n" | |
| f"- Tide level: {current_tide_level} meters\n\n" | |
| f"Include the following sections:\n" | |
| f"1. **Current Prediction**: How the current planetary and moon data affect Cancer today.\n" | |
| f"2. **Future Prediction**: Forecast what Cancer might experience tomorrow.\n" | |
| f"3. **Recommendations**: Personalized advice for Cancer focusing on emotional, personal, and professional life.\n" | |
| f"The tone should be inspirational and motivational." | |
| ) | |
| # Call OpenAI to generate predictions | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=300, | |
| temperature=0.7 | |
| ) | |
| predictions = response.choices[0].message['content'].strip().split("\n\n") | |
| current_prediction = predictions[0] if len(predictions) > 0 else "No current prediction available." | |
| future_prediction = predictions[1] if len(predictions) > 1 else "No future prediction available." | |
| # Save horoscope to a file | |
| with open("cancer_horoscope.txt", "w") as file: | |
| file.write("Cancer Horoscope Report\n") | |
| file.write("========================\n") | |
| file.write(f"Current Prediction:\n{current_prediction}\n\n") | |
| file.write(f"Future Prediction:\n{future_prediction}\n") | |
| return {"current_prediction": current_prediction, "future_prediction": future_prediction} | |
| except Exception as e: | |
| logging.error(f"Error generating Cancer prediction: {str(e)}") | |
| return {"error": f"Unable to generate prediction: {str(e)}"} | |
| cancer_agent = Agent( | |
| name="CancerAgent", | |
| instructions="Generate detailed horoscope reports for Cancer using OpenAI.", | |
| functions=[generate_cancer_prediction] | |
| ) |