Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
from flask import Flask, request, jsonify, render_template
|
2 |
-
import requests
|
3 |
-
import base64
|
4 |
-
|
5 |
-
app = Flask(__name__)
|
6 |
-
|
7 |
-
# URL do modelo Hugging Face e seu token de API
|
8 |
-
HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
9 |
-
HUGGINGFACE_API_TOKEN = "SEU_TOKEN_AQUI"
|
10 |
-
|
11 |
-
headers = {
|
12 |
-
"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"
|
13 |
-
}
|
14 |
-
|
15 |
-
@app.route("/")
|
16 |
-
def home():
|
17 |
-
return """
|
18 |
-
<h1>Gerador de Imagens</h1>
|
19 |
-
<form action="/generate" method="post" enctype="multipart/form-data">
|
20 |
-
<label for="prompt">Descri莽茫o:</label>
|
21 |
-
<input type="text" name="prompt" id="prompt" required>
|
22 |
-
<button type="submit">Gerar</button>
|
23 |
-
</form>
|
24 |
-
"""
|
25 |
-
|
26 |
-
@app.route("/generate", methods=["POST"])
|
27 |
-
def generate_image():
|
28 |
-
prompt = request.form.get("prompt")
|
29 |
-
if not prompt:
|
30 |
-
return jsonify({"error": "O campo 'prompt' 茅 obrigat贸rio."}), 400
|
31 |
-
|
32 |
-
# Requisi莽茫o para o modelo Hugging Face
|
33 |
-
response = requests.post(
|
34 |
-
HUGGINGFACE_API_URL,
|
35 |
-
headers=headers,
|
36 |
-
json={"inputs": prompt}
|
37 |
-
)
|
38 |
-
|
39 |
-
if response.status_code != 200:
|
40 |
-
return jsonify({"error": "Erro ao gerar a imagem", "details": response.text}), 500
|
41 |
-
|
42 |
-
# Converte imagem para Base64
|
43 |
-
image_data = response.content
|
44 |
-
image_base64 = base64.b64encode(image_data).decode("utf-8")
|
45 |
-
|
46 |
-
# Retorna a imagem diretamente no navegador
|
47 |
-
return f'<img src="data:image/png;base64,{image_base64}" alt="Generated Image">'
|
48 |
-
|
49 |
-
if __name__ == "__main__":
|
50 |
-
from waitress import serve
|
51 |
-
serve(app, host="0.0.0.0", port=8080)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|