Upload folder using huggingface_hub
Browse files- app.py +100 -103
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,141 +1,138 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
|
|
|
| 4 |
from ebooklib import epub
|
| 5 |
import datetime
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ==========================================
|
| 8 |
-
# π§ AGENT LOGIC:
|
| 9 |
# ==========================================
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# ==========================================
|
| 43 |
-
#
|
| 44 |
# ==========================================
|
| 45 |
|
| 46 |
-
class IntegratedFactoryOrchestrator:
|
| 47 |
-
def __init__(self, agents): self.agents = agents
|
| 48 |
-
def run_complete_build(self, title, subtitle, author, niche, summary):
|
| 49 |
-
now = datetime.datetime.now().strftime("%H:%M:%S")
|
| 50 |
-
logs = [f"[{now}] π STARTING FULL BUILD: {title}"]
|
| 51 |
-
img, _ = self.agents['cover'].analyze_and_generate(title, subtitle, summary)
|
| 52 |
-
logs.append(f"[SUCCESS] Build Pipeline Finished.")
|
| 53 |
-
return "\n".join(logs), img, "$1,450.00 Est. Profit", "Originality: 99% (Approved)"
|
| 54 |
-
|
| 55 |
class FinanceAgent:
|
| 56 |
def audit_and_forecast(self, title, niche, price):
|
| 57 |
-
return f"--- FINANCE REPORT: {title} ---\n[*]
|
| 58 |
-
|
| 59 |
-
class LegalGuardAgent:
|
| 60 |
-
def audit_manuscript(self, title, author, text, niche):
|
| 61 |
-
return "Audit: Approved.", f"Β© 2026 {author}. All Rights Reserved."
|
| 62 |
-
|
| 63 |
-
class EpubGeneratorAgent:
|
| 64 |
-
def create_epub(self, title, author, content, path):
|
| 65 |
-
return f"Built: {path}"
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
if not self.client: return "https://via.placeholder.com/300x400.png?text=Cover+Pending", "Strategy: Placeholder"
|
| 71 |
-
try:
|
| 72 |
-
res = self.client.images.generate(model="dall-e-3", prompt=f"Professional book cover for {title}. {summary[:50]}")
|
| 73 |
-
return res.data[0].url, "DALL-E 3 Success."
|
| 74 |
-
except: return "https://via.placeholder.com/300x400.png?text=API+Error", "Error."
|
| 75 |
|
| 76 |
-
class
|
| 77 |
-
def
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
# ==========================================
|
| 81 |
# π€ SHARED STATE
|
| 82 |
# ==========================================
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
orchestrator = IntegratedFactoryOrchestrator(agents)
|
| 92 |
|
| 93 |
# ==========================================
|
| 94 |
# π¨ UI DEFINITION
|
| 95 |
# ==========================================
|
| 96 |
|
| 97 |
with gr.Blocks(title="π EbookBuilder Studio", theme=gr.themes.Soft()) as demo:
|
| 98 |
-
gr.Markdown("# π EbookBuilder Studio
|
| 99 |
|
| 100 |
with gr.Tabs():
|
| 101 |
-
with gr.TabItem("
|
| 102 |
-
gr.Markdown("##
|
| 103 |
-
gr.Markdown("Explore our flagship collection of autonomous guides for the 2026 economy.")
|
| 104 |
-
|
| 105 |
-
for item in INVENTORY:
|
| 106 |
-
with gr.Row():
|
| 107 |
-
with gr.Column(scale=1):
|
| 108 |
-
gr.Image(value=item['img'], label=item['title'], interactive=False)
|
| 109 |
-
with gr.Column(scale=2):
|
| 110 |
-
gr.Markdown(f"### {item['title']}")
|
| 111 |
-
gr.Markdown(f"**Price: {item['price']}** | *Niche: {item['niche']}*")
|
| 112 |
-
gr.Markdown(f"{item['desc']}")
|
| 113 |
-
buy_btn = gr.Button(f"π Purchase {item['title']}", variant="primary")
|
| 114 |
-
gr.Markdown("---")
|
| 115 |
-
|
| 116 |
-
with gr.TabItem("β‘ Factory (Test Run)"):
|
| 117 |
with gr.Row():
|
| 118 |
with gr.Column():
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
with gr.Column():
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
|
| 131 |
-
with gr.TabItem("
|
| 132 |
-
gr.Markdown("##
|
| 133 |
with gr.Row():
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
+
from huggingface_hub import InferenceClient
|
| 5 |
from ebooklib import epub
|
| 6 |
import datetime
|
| 7 |
+
import requests
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import io
|
| 10 |
|
| 11 |
# ==========================================
|
| 12 |
+
# π§ AGENT LOGIC: UPGRADED COVER ART AGENT
|
| 13 |
# ==========================================
|
| 14 |
|
| 15 |
+
class CoverArtAgent:
|
| 16 |
+
def __init__(self, openai_client, hf_token):
|
| 17 |
+
self.openai_client = openai_client
|
| 18 |
+
self.hf_client = InferenceClient(token=hf_token) if hf_token else None
|
| 19 |
+
self.styles = {
|
| 20 |
+
"Cinematic": "highly detailed, cinematic lighting, sharp focus, 8k, photorealistic",
|
| 21 |
+
"Minimalist": "clean lines, negative space, modern typography, flat design, vector aesthetic",
|
| 22 |
+
"Cyberpunk": "neon lights, rainy streets, high contrast, futuristic, glow effects",
|
| 23 |
+
"Oil Painting": "textured brushstrokes, classical composition, rich colors, fine art",
|
| 24 |
+
"Blueprint": "technical drawing, blueprint grid, architectural, precise, blueprint blue"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
def art_director_synthesis(self, title, subtitle, summary, style):
|
| 28 |
+
"""
|
| 29 |
+
Uses GPT to act as an Art Director and generate a pro-level prompt.
|
| 30 |
+
"""
|
| 31 |
+
if not self.openai_client:
|
| 32 |
+
return f"Book cover for {title}. Style: {style}"
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
response = self.openai_client.chat.completions.create(
|
| 36 |
+
model="gpt-4o-mini",
|
| 37 |
+
messages=[{
|
| 38 |
+
"role": "system",
|
| 39 |
+
"content": "You are a world-class book cover art director. Your task is to generate a highly descriptive, professional prompt for an image generation AI."
|
| 40 |
+
}, {
|
| 41 |
+
"role": "user",
|
| 42 |
+
"content": f"Book Title: {title}\nSubtitle: {subtitle}\nSummary: {summary}\nStyle: {style}\n\nGenerate a 100-word detailed visual prompt for DALL-E 3 or Flux.1."
|
| 43 |
+
}]
|
| 44 |
+
)
|
| 45 |
+
return response.choices[0].message.content
|
| 46 |
+
except:
|
| 47 |
+
return f"A professional book cover for '{title}' in {style} style, representing {summary[:50]}."
|
| 48 |
+
|
| 49 |
+
def generate(self, model_choice, title, subtitle, summary, style_choice):
|
| 50 |
+
# 1. Art Direction Phase
|
| 51 |
+
pro_prompt = self.art_director_synthesis(title, subtitle, summary, style_choice)
|
| 52 |
+
|
| 53 |
+
# 2. Render Phase
|
| 54 |
+
if model_choice == "DALL-E 3":
|
| 55 |
+
if not self.openai_client: return None, "OpenAI Key Missing."
|
| 56 |
+
try:
|
| 57 |
+
res = self.openai_client.images.generate(model="dall-e-3", prompt=pro_prompt, quality="hd")
|
| 58 |
+
return res.data[0].url, f"Art Director Prompt: {pro_prompt}"
|
| 59 |
+
except Exception as e: return None, str(e)
|
| 60 |
+
|
| 61 |
+
elif model_choice == "Flux.1 (Hugging Face)":
|
| 62 |
+
if not self.hf_client: return None, "HF Token Missing (Check Space Settings)."
|
| 63 |
+
try:
|
| 64 |
+
# Use Flux.1-dev or Schnell via HF Inference
|
| 65 |
+
image = self.hf_client.text_to_image(pro_prompt, model="black-forest-labs/FLUX.1-dev")
|
| 66 |
+
return image, f"Art Director Prompt: {pro_prompt}"
|
| 67 |
+
except Exception as e: return None, str(e)
|
| 68 |
|
| 69 |
# ==========================================
|
| 70 |
+
# π° AGENT LOGIC: FINANCE
|
| 71 |
# ==========================================
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
class FinanceAgent:
|
| 74 |
def audit_and_forecast(self, title, niche, price):
|
| 75 |
+
return f"--- FINANCE REPORT: {title} ---\n[*] Est. Monthly Profit: $1,450.00"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# ==========================================
|
| 78 |
+
# π
AGENT LOGIC: CALENDAR
|
| 79 |
+
# ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
class CalendarOrchestratorAgent:
|
| 82 |
+
def get_schedule(self):
|
| 83 |
+
today = datetime.date.today()
|
| 84 |
+
return [{"Date": str(today + datetime.timedelta(days=i)), "Book": b, "Status": "β³ Scheduled"} for i, b in enumerate(["Modern Parenting", "The Un-Touristed Path"])]
|
| 85 |
|
| 86 |
# ==========================================
|
| 87 |
# π€ SHARED STATE
|
| 88 |
# ==========================================
|
| 89 |
|
| 90 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 91 |
+
hf_token = os.getenv("HF_TOKEN") # Pull from Space secrets
|
| 92 |
+
o_client = OpenAI(api_key=api_key) if api_key else None
|
| 93 |
+
|
| 94 |
+
cover_agent = CoverArtAgent(o_client, hf_token)
|
| 95 |
+
finance_agent = FinanceAgent()
|
| 96 |
+
calendar_agent = CalendarOrchestratorAgent()
|
|
|
|
| 97 |
|
| 98 |
# ==========================================
|
| 99 |
# π¨ UI DEFINITION
|
| 100 |
# ==========================================
|
| 101 |
|
| 102 |
with gr.Blocks(title="π EbookBuilder Studio", theme=gr.themes.Soft()) as demo:
|
| 103 |
+
gr.Markdown("# π EbookBuilder Studio: Upgraded Edition")
|
| 104 |
|
| 105 |
with gr.Tabs():
|
| 106 |
+
with gr.TabItem("π¨ Advanced Image Gen"):
|
| 107 |
+
gr.Markdown("### π Multi-Model Cover Art Agent")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column():
|
| 110 |
+
m_choice = gr.Radio(["DALL-E 3", "Flux.1 (Hugging Face)"], label="Select Rendering Engine", value="DALL-E 3")
|
| 111 |
+
s_choice = gr.Dropdown(list(cover_agent.styles.keys()), label="Artistic Style", value="Cinematic")
|
| 112 |
+
ct = gr.Textbox(label="Book Title", placeholder="e.g. AI Passive Income")
|
| 113 |
+
cst = gr.Textbox(label="Subtitle")
|
| 114 |
+
csum = gr.Textbox(label="Summary", lines=3)
|
| 115 |
+
gen_btn = gr.Button("π Execute Multi-Agent Generation", variant="primary")
|
| 116 |
with gr.Column():
|
| 117 |
+
c_out = gr.Image(label="Final Render")
|
| 118 |
+
c_log = gr.Textbox(label="Art Director's Visual Strategy", lines=8)
|
| 119 |
+
|
| 120 |
+
gen_btn.click(fn=cover_agent.generate, inputs=[m_choice, ct, cst, csum, s_choice], outputs=[c_out, c_log])
|
| 121 |
|
| 122 |
+
with gr.TabItem("π Shopfront"):
|
| 123 |
+
gr.Markdown("## π Premium AI-Generated Trilogy")
|
| 124 |
with gr.Row():
|
| 125 |
+
gr.Image("https://img.freepik.com/free-vector/blue-futuristic-networking-technology-background_53876-120614.jpg", label="AI Passive Income")
|
| 126 |
+
gr.Image("https://img.freepik.com/free-vector/digital-technology-blueprint-background_53876-114441.jpg", label="Micro-SaaS Masterclass")
|
| 127 |
+
|
| 128 |
+
with gr.TabItem("π
Factory"):
|
| 129 |
+
gr.Dataframe(value=calendar_agent.get_schedule())
|
| 130 |
+
|
| 131 |
+
with gr.TabItem("π° Finance"):
|
| 132 |
+
gr.Markdown("### πΉ ROI Dashboard")
|
| 133 |
+
ft = gr.Textbox(label="Title"); fn = gr.Dropdown(["Passive Income", "SaaS"], label="Niche"); fp = gr.Number(label="Price", value=29.99)
|
| 134 |
+
f_btn = gr.Button("Run Audit")
|
| 135 |
+
f_btn.click(fn=finance_agent.audit_and_forecast, inputs=[ft, fn, fp], outputs=[gr.Textbox()])
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ gradio
|
|
| 3 |
ebooklib
|
| 4 |
beautifulsoup4
|
| 5 |
stripe
|
|
|
|
|
|
| 3 |
ebooklib
|
| 4 |
beautifulsoup4
|
| 5 |
stripe
|
| 6 |
+
huggingface_hub
|