Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
| 3 |
from huggingface_hub import login
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 12 |
-
|
| 13 |
-
# Configurar el pipeline de generaci贸n de texto
|
| 14 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Funci贸n para generar texto con un contador de mensajes
|
| 21 |
def generate_text(prompt, history, user_id):
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Generar la respuesta
|
| 29 |
response = generator(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.9)[0]['generated_text']
|
| 30 |
-
|
| 31 |
-
history.append([prompt, response]) # A帽adir el mensaje al historial de conversaci贸n
|
| 32 |
return history
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with gr.Blocks() as iface:
|
| 36 |
-
gr.Markdown("#
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
prompt = gr.Textbox(label="Escribe tu mensaje aqu铆...")
|
| 40 |
-
|
| 41 |
submit_button = gr.Button("Enviar")
|
| 42 |
-
clear_button = gr.Button("
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
from firebase_admin import auth, firestore, initialize_app
|
| 4 |
from huggingface_hub import login
|
| 5 |
+
import stripe
|
| 6 |
|
| 7 |
+
# Configurar Firebase y Stripe
|
| 8 |
+
initialize_app() # Inicializar Firebase con credenciales
|
| 9 |
+
db = firestore.client() # Cliente de Firestore
|
| 10 |
+
stripe.api_key = "TU_CLAVE_SECRETA_STRIPE"
|
| 11 |
|
| 12 |
+
# Autenticaci贸n de Hugging Face
|
| 13 |
+
login("hf_...gxEF")
|
| 14 |
+
|
| 15 |
+
# Configuraci贸n del modelo
|
| 16 |
+
model_name = "meta-llama/Llama-2-7b-chat-hf"
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
| 19 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
|
| 21 |
+
# L铆mite de mensajes gratis
|
| 22 |
+
FREE_MESSAGE_LIMIT = 15
|
| 23 |
+
|
| 24 |
+
def sign_in_with_email(email, password):
|
| 25 |
+
user = auth.get_user_by_email(email)
|
| 26 |
+
if user:
|
| 27 |
+
# Iniciar sesi贸n y devolver detalles del usuario
|
| 28 |
+
return {"user_id": user.uid, "message_count": user.custom_claims.get("message_count", 0)}
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
def sign_up_with_email(email, password, username):
|
| 32 |
+
user = auth.create_user(email=email, password=password, display_name=username)
|
| 33 |
+
db.collection("users").document(user.uid).set({"email": email, "username": username, "message_count": FREE_MESSAGE_LIMIT})
|
| 34 |
+
return {"user_id": user.uid, "message_count": FREE_MESSAGE_LIMIT}
|
| 35 |
+
|
| 36 |
+
def check_subscription(user_id):
|
| 37 |
+
user_ref = db.collection("users").document(user_id)
|
| 38 |
+
user_data = user_ref.get().to_dict()
|
| 39 |
+
if user_data and user_data.get("subscribed", False):
|
| 40 |
+
return True
|
| 41 |
+
return False
|
| 42 |
|
|
|
|
| 43 |
def generate_text(prompt, history, user_id):
|
| 44 |
+
user_ref = db.collection("users").document(user_id)
|
| 45 |
+
user_data = user_ref.get().to_dict()
|
| 46 |
|
| 47 |
+
# Verificar suscripci贸n y l铆mite de mensajes
|
| 48 |
+
if not user_data.get("subscribed", False):
|
| 49 |
+
if user_data["message_count"] <= 0:
|
| 50 |
+
return history + [[prompt, "Has alcanzado el l铆mite de mensajes gratuitos. Suscr铆bete para continuar."]]
|
| 51 |
+
user_ref.update({"message_count": user_data["message_count"] - 1})
|
| 52 |
|
|
|
|
| 53 |
response = generator(prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.9)[0]['generated_text']
|
| 54 |
+
history.append([prompt, response])
|
|
|
|
| 55 |
return history
|
| 56 |
|
| 57 |
+
def create_subscription(user_id):
|
| 58 |
+
session = stripe.checkout.Session.create(
|
| 59 |
+
payment_method_types=['card'],
|
| 60 |
+
line_items=[{
|
| 61 |
+
'price': 'precio_ID', # Reemplaza con el ID de tu precio en Stripe
|
| 62 |
+
'quantity': 1,
|
| 63 |
+
}],
|
| 64 |
+
mode='subscription',
|
| 65 |
+
success_url='https://tu-app/success', # URL de 茅xito
|
| 66 |
+
cancel_url='https://tu-app/cancel', # URL de cancelaci贸n
|
| 67 |
+
)
|
| 68 |
+
return session.url
|
| 69 |
+
|
| 70 |
+
# Interfaz Gradio
|
| 71 |
with gr.Blocks() as iface:
|
| 72 |
+
gr.Markdown("# Bienvenido a Amsterdam IA")
|
| 73 |
+
|
| 74 |
+
# Registro e Inicio de Sesi贸n
|
| 75 |
+
with gr.Tab("Iniciar Sesi贸n"):
|
| 76 |
+
email_login = gr.Textbox(label="Correo")
|
| 77 |
+
password_login = gr.Textbox(label="Contrase帽a", type="password")
|
| 78 |
+
login_button = gr.Button("Iniciar Sesi贸n")
|
| 79 |
+
|
| 80 |
+
with gr.Tab("Crear Cuenta"):
|
| 81 |
+
email_signup = gr.Textbox(label="Correo")
|
| 82 |
+
password_signup = gr.Textbox(label="Contrase帽a", type="password")
|
| 83 |
+
username_signup = gr.Textbox(label="Nombre de Usuario")
|
| 84 |
+
signup_button = gr.Button("Crear Cuenta")
|
| 85 |
+
|
| 86 |
+
# Chat
|
| 87 |
+
chatbot = gr.Chatbot(label="Chat con Amsterdam IA")
|
| 88 |
prompt = gr.Textbox(label="Escribe tu mensaje aqu铆...")
|
|
|
|
| 89 |
submit_button = gr.Button("Enviar")
|
| 90 |
+
clear_button = gr.Button("Nuevo Chat")
|
| 91 |
|
| 92 |
+
login_button.click(sign_in_with_email, inputs=[email_login, password_login], outputs=[chatbot])
|
| 93 |
+
signup_button.click(sign_up_with_email, inputs=[email_signup, password_signup, username_signup], outputs=[chatbot])
|
| 94 |
+
submit_button.click(generate_text, inputs=[prompt, chatbot, email_login], outputs=chatbot)
|
| 95 |
+
clear_button.click(fn=lambda: None, inputs=None, outputs=chatbot)
|
| 96 |
|
| 97 |
iface.launch()
|