from fastapi import FastAPI, Request, Form from fastapi.responses import HTMLResponse, FileResponse from jinja2 import Environment, DictLoader from diffusers import StableDiffusionPipeline from transformers import pipeline from PIL import Image from imageio.v2 import ImageSequenceClip import pyttsx3 import matplotlib.pyplot as plt import torch import os app = FastAPI() # Cartelle statiche for folder in ["immagini", "video", "musica", "voce", "grafici"]: os.makedirs(f"static/{folder}", exist_ok=True) # Modelli image_gen = StableDiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-2-1", torch_dtype=torch.float32 ).to("cpu") music_gen = pipeline("text-to-audio", model="facebook/musicgen-small") chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.2") # HTML template templates = { "index.html": """ Giorgio's Intelligence

GIORGIO’S INTELLIGENCE

{% if file_path %}
{{ preview|safe }}

📥 Scarica file

{% endif %} """ } env = Environment(loader=DictLoader(templates)) @app.get("/", response_class=HTMLResponse) async def home(request: Request): html = env.get_template("index.html").render(file_path=None, preview=None) return HTMLResponse(content=html) @app.post("/", response_class=HTMLResponse) async def generate(request: Request, prompt: str = Form(...), action: str = Form(...)): safe_name = prompt.replace(" ", "_").replace("/", "_") file_path, preview = None, "" if action == "immagine": image = image_gen(prompt).images[0] file_path = f"static/immagini/{safe_name}.png" image.save(file_path) preview = f'' elif action == "video": frames = [] for i in range(5): img = image_gen(f"{prompt}, scena {i+1}").images[0] img_path = f"static/video/{safe_name}_frame{i}.png" img.save(img_path) frames.append(img_path) clip = ImageSequenceClip(frames, fps=1) file_path = f"static/video/{safe_name}.mp4" clip.write_videofile(file_path, codec="libx264", audio=False) preview = f'' elif action == "musica": audio = music_gen(prompt)[0]["audio"] file_path = f"static/musica/{safe_name}.wav" with open(file_path, "wb") as f: f.write(audio) preview = f'' elif action == "voce": file_path = f"static/voce/{safe_name}.mp3" engine = pyttsx3.init() engine.save_to_file(prompt, file_path) engine.runAndWait() preview = f'' elif action == "grafico": file_path = f"static/grafici/{safe_name}.png" plt.figure() plt.plot([1, 2, 3, 4], [1, 4, 2, 3]) plt.title(prompt) plt.savefig(file_path) preview = f'' elif action == "chat": risposta = chatbot(prompt, max_new_tokens=100)[0]["generated_text"] preview = f"

Risposta: {risposta}

" html = env.get_template("index.html").render(file_path=file_path, preview=preview) return HTMLResponse(content=html) @app.get("/static/{folder}/{filename}", response_class=FileResponse) async def serve_file(folder: str, filename: str): return FileResponse(path=f"static/{folder}/{filename}")