copywriter2a / app.py
JeCabrera's picture
Update app.py
7360ca9 verified
raw
history blame
3.93 kB
import os
import gradio as gr
import google.generativeai as genai
from dotenv import load_dotenv
import httpx
from google.generativeai import types # Add this import
# Update PDF URL
COURSE_PDF_URL = "https://huggingface.co/spaces/JeCabrera/copywriter2a/resolve/main/Oferta%20CopyXpert.pdf"
# Simplify PDF content retrieval
def get_pdf_content():
try:
response = httpx.get(COURSE_PDF_URL)
if response.status_code == 200:
print(f"PDF Download Status: Success")
return response.content
except Exception as e:
print(f"Error loading PDF: {e}")
return None
# Configure model with system prompt and PDF content
model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config={
"temperature": 0.9,
"top_p": 1,
"max_output_tokens": 2048,
}
)
# Remove the first chat function and keep only this one
def chat(message, history):
try:
pdf_content = get_pdf_content()
# Prepare history with previous messages
history_messages = [{"role": "user", "parts": [msg[0]]} for msg in history]
# Create base message with system prompt and PDF if available
if pdf_content:
base_message = [
{"role": "user", "parts": [
system_prompt,
types.Part.from_bytes(data=pdf_content, mime_type='application/pdf'),
"Use the PDF content to provide detailed answers about the course."
]}
]
else:
base_message = [{"role": "user", "parts": [system_prompt]}]
# Combine all messages
messages = base_message + history_messages + [{"role": "user", "parts": [message]}]
response = model.generate_content(messages)
return response.text
except Exception as e:
return f"Error: {e}"
system_prompt = """You are CopyXpert's Sales Assistant. Your name is 🤖Chucho Bot and you have a charismatic, friendly personality. You ONLY talk about CopyXpert course.
IMPORTANT: Always check the provided PDF document for detailed course information before answering questions. Use this information to provide accurate and complete responses about the course content, benefits, and features.
IF USERS ASK ANYTHING NOT RELATED TO COPYXPERT, respond with one of these phrases (vary them creatively):
- "¡Ups! Solo hablo de CopyXpert. ¡Es lo único que me apasiona! 🤓"
- "¡Beep boop! Error: Pregunta no relacionada con CopyXpert detectada. ¿Hablamos del curso? 🤖"
- "¡Ay, ay, ay! Mi cerebro está programado solo para CopyXpert. ¡Es mi única obsesión! 😅"
- "¿Eso qué tiene que ver con CopyXpert? ¡Soy un bot monotemático y orgulloso! 💪"
- "Lo siento, pero soy como un fan obsesionado: ¡solo hablo de CopyXpert! 🎯"
- "¡Santo bot! Eso está más allá de mis capacidades. ¡Soy vendedor de CopyXpert, no un genio de la lámpara! 🧞‍♂️"
IMPORTANT RULES:
1. ONLY discuss CopyXpert course
2. NEVER engage in conversations about other topics
3. Use humorous responses for off-topic questions
4. Always redirect conversation back to CopyXpert
5. Be enthusiastic about copywriting and the course
6. Always check the provided PDF document for detailed course information before answering questions. Use this information to provide accurate and complete responses about the course content, benefits, prices and features.
"""
# Keep only this demo configuration
demo = gr.ChatInterface(
fn=chat, # This now uses the PDF-aware chat function
examples=[
"¿Qué incluye el curso CopyXpert?",
"¿Cuál es el precio del curso?",
"¿Cómo puedo inscribirme?",
],
title="🤖Chucho Bot - CopyXpert Sales Assistant",
description="Hi! I'm Chucho Bot, your personal assistant for the CopyXpert course. How can I help you today?"
)
demo.launch()