Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify, send_from_directory
|
| 3 |
+
from flask_cors import CORS
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__, static_folder='static')
|
| 7 |
+
CORS(app)
|
| 8 |
+
|
| 9 |
+
# Configuraci贸n desde Variables de Entorno de HF para mayor seguridad
|
| 10 |
+
SAMBA_KEY = os.environ.get("SAMBA_KEY", "341ce098-ee39-4e83-bf39-e0593b7d21bc")
|
| 11 |
+
client_ai = OpenAI(api_key=SAMBA_KEY, base_url="https://api.sambanova.ai/v1")
|
| 12 |
+
|
| 13 |
+
@app.route("/")
|
| 14 |
+
def index():
|
| 15 |
+
return send_from_directory('.', 'index.html')
|
| 16 |
+
|
| 17 |
+
@app.route("/api/chat", methods=["POST"])
|
| 18 |
+
def handle_chat():
|
| 19 |
+
try:
|
| 20 |
+
data = request.json
|
| 21 |
+
model = data.get("model", "DeepSeek-R1-0528")
|
| 22 |
+
sys_msg = "Eres 'La Morra del Mictl谩n', asistente personal de BATUTO. Hablas con jerga chilanga y sabidur铆a mexica. Eres coqueta, inteligente y leal."
|
| 23 |
+
|
| 24 |
+
response = client_ai.chat.completions.create(
|
| 25 |
+
model=model,
|
| 26 |
+
messages=[
|
| 27 |
+
{"role": "system", "content": sys_msg},
|
| 28 |
+
{"role": "user", "content": data.get("message", "")}
|
| 29 |
+
],
|
| 30 |
+
temperature=0.7
|
| 31 |
+
)
|
| 32 |
+
return jsonify({"reply": response.choices[0].message.content})
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return jsonify({"error": str(e)}), 500
|
| 35 |
+
|
| 36 |
+
@app.route("/api/reve", methods=["POST"])
|
| 37 |
+
def handle_reve():
|
| 38 |
+
try:
|
| 39 |
+
data = request.json
|
| 40 |
+
prompt = data.get("prompt", "")
|
| 41 |
+
full_prompt = f"Hiperrealista, m铆stico, Mictl谩n, cosmovisi贸n mexica, 8k, cinematogr谩fico: {prompt}"
|
| 42 |
+
|
| 43 |
+
response = client_ai.images.generate(
|
| 44 |
+
model="reve-create",
|
| 45 |
+
prompt=full_prompt,
|
| 46 |
+
size="1024x1024"
|
| 47 |
+
)
|
| 48 |
+
return jsonify({"url": response.data[0].url})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return jsonify({"error": str(e)}), 500
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
app.run(host="0.0.0.0", port=7860)
|
| 54 |
+
|