Introduction
In this tutorial, you'll learn how to build a basic astrology horoscope generator using Python and an AI API. This project demonstrates how AI can be integrated into astrology applications, similar to what Midjourney is doing with Co-Star. You'll create a simple web interface that fetches AI-generated horoscopes based on zodiac signs.
Prerequisites
- Python 3.7 or higher installed
- Basic understanding of Python and web development
- API key from an AI language model service (we'll use OpenAI's API)
- Basic HTML and CSS knowledge
Step 1: Set Up Your Development Environment
Install Required Libraries
First, create a virtual environment and install the necessary packages:
python -m venv astro_env
source astro_env/bin/activate # On Windows: astro_env\Scripts\activate
pip install flask openai python-dotenv
This creates an isolated environment for our project and installs Flask for web development and OpenAI's Python library for API integration.
Step 2: Configure Your API Key
Create Environment Variables
Create a .env file in your project directory:
OPENAI_API_KEY=your_openai_api_key_here
Replace your_openai_api_key_here with your actual OpenAI API key. Never commit this file to version control.
Step 3: Create the Main Application
Build the Flask Web App
Create a file named app.py:
import os
from flask import Flask, render_template, request
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
# Zodiac signs
zodiac_signs = [
'Aries', 'Taurus', 'Gemini', 'Cancer', 'Leo', 'Virgo',
'Libra', 'Scorpio', 'Sagittarius', 'Capricorn', 'Aquarius', 'Pisces'
]
@app.route('/')
def index():
return render_template('index.html', signs=zodiac_signs)
@app.route('/horoscope', methods=['POST'])
def get_horoscope():
sign = request.form['sign']
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an expert astrologer who provides personalized daily horoscopes for each zodiac sign. Keep responses concise and engaging."},
{"role": "user", "content": f"Generate a personalized daily horoscope for {sign}. Include insights about love, career, and health. Keep it under 150 words."}
],
max_tokens=200,
temperature=0.7
)
horoscope = response.choices[0].message.content
return render_template('result.html', sign=sign, horoscope=horoscope)
except Exception as e:
return f"Error generating horoscope: {str(e)}"
if __name__ == '__main__':
app.run(debug=True)
This code sets up a Flask web application that accepts zodiac sign input and generates AI-powered horoscopes using OpenAI's API.
Step 4: Create HTML Templates
Build the User Interface
Create a templates directory and add index.html:
<!DOCTYPE html>
<html>
<head>
<title>AI Astrology Horoscope</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
select, button { padding: 10px; margin: 10px 0; width: 100%; }
.container { background: #f5f5f5; padding: 20px; border-radius: 10px; }
</style>
</head>
<body>
<div class="container">
<h1>AI Astrology Horoscope</h1>
<p>Discover your daily horoscope powered by AI</p>
<form action="/horoscope" method="post">
<label for="sign">Select Your Zodiac Sign:</label>
<select name="sign" id="sign" required>
<option value="">Choose a sign</option>
% for sign in signs %
<option value="{{ sign }}">{{ sign }}</option>
% endfor %
</select>
<button type="submit">Get My Horoscope</button>
</form>
</div>
</body>
</html>
This creates the main interface where users select their zodiac sign to generate a horoscope.
Step 5: Create the Result Page
Display Generated Horoscopes
Create templates/result.html:
<!DOCTYPE html>
<html>
<head>
<title>Your Horoscope</title>
<style>
body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; }
.horoscope { background: #e8f4f8; padding: 20px; border-radius: 10px; margin: 20px 0; }
.sign { font-weight: bold; color: #2c5e8a; }
.back-link { margin-top: 20px; }
</style>
</head>
<body>
<h1>Your Daily Horoscope</h1>
<div class="horoscope">
<p>For <span class="sign">{{ sign }}</span>:</p>
<p>{{ horoscope }}</p>
</div>
<div class="back-link">
<a href="/">Get Another Horoscope</a>
</div>
</body>
</html>
This displays the AI-generated horoscope in an attractive format with a back link to try another sign.
Step 6: Run Your Application
Start the Web Server
Run your Flask application:
python app.py
Visit http://localhost:5000 in your browser to use the astrology app. Select a zodiac sign and click "Get My Horoscope" to see AI-generated predictions.
Step 7: Enhance with Additional Features
Add Personalization Options
Enhance your application by adding:
- Date-based horoscopes (current date, next week, etc.)
- Personal name input for more personalized messages
- Multiple AI models for different tone options
- Database storage for user preferences
For example, you could modify the prompt to include:
{"role": "user", "content": f"Generate a personalized daily horoscope for {sign} born on {birth_date}. Include insights about love, career, and health. Keep it under 150 words."}
This demonstrates how Midjourney's approach to Co-Star might evolve to include more personalized AI experiences.
Summary
This tutorial showed you how to build a basic astrology horoscope generator using Python Flask and OpenAI's API. The project demonstrates how AI technology can be integrated into personalized applications like astrology, similar to how Midjourney is expanding beyond image generation into new domains. You learned to:
- Set up a Flask web application
- Connect to OpenAI's API for text generation
- Create HTML templates for user interaction
- Handle form submissions and API responses
- Display AI-generated content in a user-friendly interface
The skills you've learned can be extended to create more sophisticated AI-powered astrology applications, potentially incorporating features like personalized predictions, historical data analysis, or integration with other astrological tools.



