Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import json
|
|
|
|
| 4 |
import requests
|
| 5 |
import gradio as gr
|
| 6 |
|
|
@@ -9,57 +10,88 @@ import gradio as gr
|
|
| 9 |
# ----------------------------------------------------------------------
|
| 10 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 11 |
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
|
| 12 |
-
|
| 13 |
-
# Default model can be overridden by the user via the dropdown
|
| 14 |
-
DEFAULT_MODEL = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
|
| 15 |
-
AVAILABLE_MODELS = [
|
| 16 |
-
"llama-3.3-70b-versatile",
|
| 17 |
-
"llama-3.1-8b-instant",
|
| 18 |
-
"qwen/qwen3-32b"
|
| 19 |
-
]
|
| 20 |
|
| 21 |
# ----------------------------------------------------------------------
|
| 22 |
# CSS (will be passed to demo.launch)
|
| 23 |
# ----------------------------------------------------------------------
|
| 24 |
CSS = """
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
-
#
|
| 29 |
background-color: #1E90FF;
|
| 30 |
color: white;
|
| 31 |
-
border: none;
|
| 32 |
-
padding: 0.5rem 1rem;
|
| 33 |
-
font-size: 1rem;
|
| 34 |
-
cursor: pointer;
|
| 35 |
}
|
| 36 |
-
#
|
| 37 |
background-color: #1C86EE;
|
| 38 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
|
| 41 |
# ----------------------------------------------------------------------
|
| 42 |
-
# Helper
|
| 43 |
# ----------------------------------------------------------------------
|
| 44 |
-
def call_groq(
|
| 45 |
"""
|
| 46 |
-
|
|
|
|
| 47 |
"""
|
| 48 |
if not GROQ_API_KEY:
|
| 49 |
-
raise RuntimeError("GROQ_API_KEY not set in environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
headers = {
|
| 51 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 52 |
-
"Content-Type": "application/json"
|
| 53 |
}
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
response.raise_for_status()
|
| 61 |
data = response.json()
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Remove
|
| 65 |
if content.startswith("
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
+
import tempfile
|
| 5 |
import requests
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 10 |
# ----------------------------------------------------------------------
|
| 11 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 12 |
GROQ_ENDPOINT = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
+
MODEL_NAME = os.environ.get("MODEL_NAME", "llama-3.1-8b-instant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# ----------------------------------------------------------------------
|
| 16 |
# CSS (will be passed to demo.launch)
|
| 17 |
# ----------------------------------------------------------------------
|
| 18 |
CSS = """
|
| 19 |
+
#business-input textarea {
|
| 20 |
+
border: 1px solid #CCCCCC;
|
| 21 |
+
}
|
| 22 |
+
#business-input textarea:focus {
|
| 23 |
+
border-color: #1E90FF;
|
| 24 |
}
|
| 25 |
+
#generate-btn {
|
| 26 |
background-color: #1E90FF;
|
| 27 |
color: white;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
+
#generate-btn:hover {
|
| 30 |
background-color: #1C86EE;
|
| 31 |
}
|
| 32 |
+
#problems-card, #solutions-card, #explanation-card, #prompt-card {
|
| 33 |
+
padding: 15px;
|
| 34 |
+
border-radius: 8px;
|
| 35 |
+
margin-top: 10px;
|
| 36 |
+
}
|
| 37 |
+
#problems-card {
|
| 38 |
+
background-color: #F0F8FF;
|
| 39 |
+
}
|
| 40 |
+
#solutions-card {
|
| 41 |
+
background-color: #E6F7FF;
|
| 42 |
+
}
|
| 43 |
+
#explanation-card {
|
| 44 |
+
background-color: #FFFFFF;
|
| 45 |
+
}
|
| 46 |
+
#prompt-card {
|
| 47 |
+
background-color: #F5F5F5;
|
| 48 |
+
}
|
| 49 |
+
#download-btn {
|
| 50 |
+
margin-top: 10px;
|
| 51 |
+
}
|
| 52 |
"""
|
| 53 |
|
| 54 |
# ----------------------------------------------------------------------
|
| 55 |
+
# Helper functions
|
| 56 |
# ----------------------------------------------------------------------
|
| 57 |
+
def call_groq(business_name: str) -> dict:
|
| 58 |
"""
|
| 59 |
+
Calls the Groq API and returns a dict with keys:
|
| 60 |
+
problems, solutions, explanation, eos_prompt.
|
| 61 |
"""
|
| 62 |
if not GROQ_API_KEY:
|
| 63 |
+
raise RuntimeError("GROQ_API_KEY not set in environment")
|
| 64 |
+
|
| 65 |
+
user_prompt = f"""Eres un analista de negocios experto. Dado el nombre del negocio "{business_name}", haz una lista de los 5 problemas clasicos principales, propone una solucion para cada uno, ofrece una explicacion detallada combinada y, por ultimo, genera un prompt de EOS listo para usar.
|
| 66 |
+
|
| 67 |
+
Toda la respuesta debe estar redactada en español, pero estructurada estrictamente en el siguiente formato JSON (manteniendo las claves en ingles para la lectura del sistema):
|
| 68 |
+
{{
|
| 69 |
+
"problems": ["...", "...", "..."],
|
| 70 |
+
"solutions": ["...", "...", "..."],
|
| 71 |
+
"explanation": "...",
|
| 72 |
+
"eos_prompt": "Titulo: ...\\nContexto: ...\\nInstrucciones: ..."
|
| 73 |
+
}}
|
| 74 |
+
Devuelve unica y exclusivamente el objeto JSON, sin texto introductorio ni conclusiones."""
|
| 75 |
+
payload = {
|
| 76 |
+
"model": MODEL_NAME,
|
| 77 |
+
"messages": [{"role": "user", "content": user_prompt}],
|
| 78 |
+
"temperature": 0.7,
|
| 79 |
+
"max_tokens": 1024,
|
| 80 |
+
}
|
| 81 |
headers = {
|
| 82 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 83 |
+
"Content-Type": "application/json",
|
| 84 |
}
|
| 85 |
+
|
| 86 |
+
response = requests.post(
|
| 87 |
+
GROQ_ENDPOINT,
|
| 88 |
+
headers=headers,
|
| 89 |
+
json=payload,
|
| 90 |
+
timeout=30,
|
| 91 |
+
)
|
| 92 |
response.raise_for_status()
|
| 93 |
data = response.json()
|
| 94 |
+
content = data["choices"][0]["message"]["content"].strip()
|
| 95 |
+
|
| 96 |
+
# Remove possible markdown fences
|
| 97 |
if content.startswith("
|