LogoLogo
  • Spider AI’s Artistic Transformation – Capturing Life's Beautiful Moments Through AI
  • Spider AI Updates – The Central Hub for Artistic Transformation
  • Spider Open Source Code
Powered by GitBook
On this page

Spider Open Source Code

python
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from transformers import pipeline
import openai

app = Flask(__name__)

# Configure the database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///artworks.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

# Define the Artwork model
class Artwork(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String(500), nullable=False)
    art_link = db.Column(db.String(200), nullable=False)
    style = db.Column(db.String(50), nullable=False)

# Use a pre-trained model for text generation
text_generator = pipeline("text-generation", model="gpt-2")

# Available art styles
art_styles = {
    "Impressionism": "Impressionistic representation of",
    "Abstract": "Abstract interpretation of",
    "Realism": "Realistic depiction of",
    "Surrealism": "Surrealistic vision of",
}

@app.route('/generate_art', methods=['POST'])
def generate_art():
    # Get user input
    user_description = request.json.get('description')
    selected_style = request.json.get('style', 'Realism')  # Default to realism

    # Create the art generation prompt
    art_prompt = f"{art_styles.get(selected_style, 'Realistic depiction of')} {user_description}"

    # Generate the art description
    art_description = text_generator(art_prompt, max_length=50)[0]['generated_text']
    
    # Generate a hypothetical art link
    art_link = f"http://example.com/art/{hash(art_description)}"  # Generate a link for the artwork
    
    # Save the artwork to the database
    new_artwork = Artwork(description=art_description, art_link=art_link, style=selected_style)
    db.session.add(new_artwork)
    db.session.commit()
    
    return jsonify({
        'description': art_description,
        'art_link': art_link
    })

@app.route('/artworks', methods=['GET'])
def get_artworks():
    # Retrieve all artworks
    artworks = Artwork.query.all()
    return jsonify([{
        'description': art.description,
        'art_link': art.art_link,
        'style': art.style
    } for art in artworks])

if __name__ == '__main__':
    db.create_all()  # Create the database tables
    app.run(debug=True)

Usage Instructions

  1. Environment Setup: Ensure you have installed the Flask, Flask-SQLAlchemy, and transformers libraries.

bash
pip install Flask Flask-SQLAlchemy transformers
  1. Set Up OpenAI API: Add your OpenAI API key configuration in the code.

python
openai.api_key = 'YOUR_API_KEY'
  1. Run the Application

bash
python app.py
  1. Send Request to Generate Art: Use Postman or curl to send a POST request to http://127.0.0.1:5000/generate_art, including the user description and selected style in the request body.

json
    "description": "A serene sunset over the mountains.",
    "style": "Impressionism"
}
  1. Retrieve Artworks: Send a GET request to http://127.0.0.1:5000/artworks to get all generated artworks.

Notes

  • This example creates an SQLite database to store generated artworks, supporting real-time updates.

  • You can expand the options for art styles as needed.

  • Make sure the code runs in an appropriate environment with the required libraries configured.

PreviousSpider AI Updates – The Central Hub for Artistic Transformation

Last updated 7 months ago