Spaces:
Runtime error
Runtime error
| import os | |
| import json | |
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| from firebase_admin import auth, firestore, initialize_app, credentials | |
| from huggingface_hub import login | |
| import stripe | |
| # Leer el token de Hugging Face desde una variable de entorno | |
| hf_token = os.getenv("HF_API_TOKEN") | |
| if not hf_token: | |
| raise ValueError("No se encontr贸 la variable de entorno 'HF_API_TOKEN'. Aseg煤rate de haber configurado el secreto correctamente.") | |
| # Inicializar las credenciales de Firebase | |
| cred = credentials.Certificate("amsterdam-ai-firebase-adminsdk-m8kjd-95ae3fb6bb.json") # Ruta del archivo JSON | |
| initialize_app(cred) # Inicializa la aplicaci贸n de Firebase con las credenciales | |
| # Configurar el cliente de Firestore | |
| db = firestore.client() # Esto te permite acceder a la base de datos Firestore | |
| # Configuraci贸n de Stripe con tu clave secreta | |
| stripe.api_key = "TU_CLAVE_SECRETA_STRIPE" # Reemplaza con tu clave de Stripe | |
| # Autenticaci贸n de Hugging Face usando el token le铆do de la variable de entorno | |
| login(hf_token) # Autenticar usando el token seguro | |
| # Cargar el modelo de Hugging Face | |
| model_name = "meta-llama/Llama-2-7b-chat-hf" # Puedes cambiarlo por cualquier modelo que prefieras | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name) | |
| generator = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
| # L铆mite de mensajes gratis | |
| FREE_MESSAGE_LIMIT = 15 | |
| # Funci贸n para generar texto con un contador de mensajes | |
| def generate_text(prompt, history, user_id): | |
| user_ref = db.collection("users").document(user_id) | |
| user_data = user_ref.get().to_dict() | |
| # Verificar suscripci贸n y l铆mite de mensajes | |
| if not user_data.get("subscribed", False): | |
| if user_data["message_count"] <= 0: | |
| return history + [[prompt, "Has alcanzado el l铆mite de mensajes gratuitos. Suscr铆bete para continuar."]] | |
| user_ref.update({"message_count": user_data["message_count"] - 1}) | |
| response = generator(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.9)[0]['generated_text'] | |
| history.append([prompt, response]) | |
| return history | |
| # Crear la interfaz de usuario en Gradio | |
| with gr.Blocks() as iface: | |
| gr.Markdown("# Venice IA Chatbot") | |
| user_id = gr.Textbox(label="ID de Usuario (email o identificador 煤nico)", placeholder="Introduce tu ID de usuario", visible=False) | |
| chatbot = gr.Chatbot(label="Chat con Venice IA") | |
| prompt = gr.Textbox(label="Escribe tu mensaje aqu铆...") | |
| submit_button = gr.Button("Enviar") | |
| clear_button = gr.Button("Nuevo Chat") | |
| # Funciones para los botones | |
| submit_button.click(fn=generate_text, inputs=[prompt, chatbot, user_id], outputs=chatbot) | |
| clear_button.click(fn=lambda: None, inputs=None, outputs=chatbot) | |
| iface.launch() | |