|
|
""" |
|
|
Script para integrar el Space de Hugging Face con la aplicaci贸n principal |
|
|
""" |
|
|
|
|
|
import requests |
|
|
import json |
|
|
import base64 |
|
|
from PIL import Image |
|
|
import io |
|
|
|
|
|
class HuggingFaceSpaceAPI: |
|
|
def __init__(self, space_url): |
|
|
""" |
|
|
Inicializar la API del Space |
|
|
|
|
|
Args: |
|
|
space_url (str): URL del Space (ej: https://huggingface.co/spaces/tu-usuario/tu-space) |
|
|
""" |
|
|
self.space_url = space_url |
|
|
self.api_url = f"{space_url}/api" |
|
|
|
|
|
def generate_text(self, prompt, model_name="microsoft/DialoGPT-medium", max_length=100): |
|
|
""" |
|
|
Generar texto usando el modelo del Space |
|
|
|
|
|
Args: |
|
|
prompt (str): El prompt de entrada |
|
|
model_name (str): Nombre del modelo a usar |
|
|
max_length (int): Longitud m谩xima de la respuesta |
|
|
|
|
|
Returns: |
|
|
str: Texto generado |
|
|
""" |
|
|
try: |
|
|
|
|
|
endpoint = f"{self.api_url}/text" |
|
|
|
|
|
payload = { |
|
|
"prompt": prompt, |
|
|
"model_name": model_name, |
|
|
"max_length": max_length |
|
|
} |
|
|
|
|
|
response = requests.post(endpoint, json=payload, timeout=60) |
|
|
response.raise_for_status() |
|
|
|
|
|
return response.json()["response"] |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error generando texto: {str(e)}" |
|
|
|
|
|
def generate_image(self, prompt, model_name="runwayml/stable-diffusion-v1-5", steps=20): |
|
|
""" |
|
|
Generar imagen usando el modelo del Space |
|
|
|
|
|
Args: |
|
|
prompt (str): El prompt de la imagen |
|
|
model_name (str): Nombre del modelo a usar |
|
|
steps (int): Pasos de inferencia |
|
|
|
|
|
Returns: |
|
|
str: URL de la imagen generada o base64 |
|
|
""" |
|
|
try: |
|
|
|
|
|
endpoint = f"{self.api_url}/image" |
|
|
|
|
|
payload = { |
|
|
"prompt": prompt, |
|
|
"model_name": model_name, |
|
|
"steps": steps |
|
|
} |
|
|
|
|
|
response = requests.post(endpoint, json=payload, timeout=120) |
|
|
response.raise_for_status() |
|
|
|
|
|
return response.json()["image_url"] |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error generando imagen: {str(e)}" |
|
|
|
|
|
def chat(self, message, history=None, model_name="microsoft/DialoGPT-medium"): |
|
|
""" |
|
|
Chat conversacional usando DialoGPT |
|
|
|
|
|
Args: |
|
|
message (str): Mensaje del usuario |
|
|
history (list): Historial de conversaci贸n |
|
|
model_name (str): Nombre del modelo |
|
|
|
|
|
Returns: |
|
|
str: Respuesta del modelo |
|
|
""" |
|
|
try: |
|
|
|
|
|
endpoint = f"{self.api_url}/chat" |
|
|
|
|
|
payload = { |
|
|
"message": message, |
|
|
"history": history or [], |
|
|
"model_name": model_name |
|
|
} |
|
|
|
|
|
response = requests.post(endpoint, json=payload, timeout=60) |
|
|
response.raise_for_status() |
|
|
|
|
|
return response.json()["response"] |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error en el chat: {str(e)}" |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
space_url = "https://huggingface.co/spaces/tu-usuario/tu-space" |
|
|
|
|
|
api = HuggingFaceSpaceAPI(space_url) |
|
|
|
|
|
|
|
|
print("Generando texto...") |
|
|
text_response = api.generate_text("Hola, 驴c贸mo est谩s?") |
|
|
print(f"Respuesta: {text_response}") |
|
|
|
|
|
|
|
|
print("\nChat...") |
|
|
chat_response = api.chat("Cu茅ntame una historia corta") |
|
|
print(f"Respuesta: {chat_response}") |
|
|
|
|
|
|
|
|
print("\nGenerando imagen...") |
|
|
image_url = api.generate_image("Un gato astronauta en el espacio") |
|
|
print(f"Imagen: {image_url}") |