Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,64 +3,34 @@ import gradio as gr
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import httpx
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# PDF
|
| 11 |
-
COURSE_PDF_URL = "https://drive.google.com/file/d/1Xf46sT9kSzSVdekSCnz3NsBodpGET1Gi/view?usp=drive_link"
|
| 12 |
-
|
| 13 |
-
# Load PDF content once at startup
|
| 14 |
def get_pdf_content():
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
file_id = COURSE_PDF_URL.split('/')[5]
|
| 18 |
-
direct_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 19 |
-
response = httpx.get(direct_url)
|
| 20 |
-
|
| 21 |
-
# Add verification
|
| 22 |
if response.status_code == 200:
|
| 23 |
-
content = response.content
|
| 24 |
-
content_type = response.headers.get('content-type', '')
|
| 25 |
-
|
| 26 |
print(f"PDF Download Status: Success")
|
| 27 |
-
|
| 28 |
-
print(f"Content Size: {len(content)} bytes")
|
| 29 |
-
|
| 30 |
-
if 'pdf' in content_type.lower() or content[:4] == b'%PDF':
|
| 31 |
-
return content
|
| 32 |
-
else:
|
| 33 |
-
print("Warning: Downloaded content might not be a PDF")
|
| 34 |
-
return None
|
| 35 |
-
else:
|
| 36 |
-
print(f"Failed to download PDF. Status code: {response.status_code}")
|
| 37 |
-
return None
|
| 38 |
-
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Error loading PDF: {e}")
|
| 41 |
return None
|
| 42 |
|
| 43 |
-
# Configure model with system prompt and PDF content
|
| 44 |
-
model = genai.GenerativeModel(
|
| 45 |
-
model_name="gemini-2.0-flash",
|
| 46 |
-
generation_config={
|
| 47 |
-
"temperature": 0.9,
|
| 48 |
-
"top_p": 1,
|
| 49 |
-
"max_output_tokens": 2048,
|
| 50 |
-
}
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
def chat(message, history):
|
| 54 |
try:
|
| 55 |
-
# Get PDF content
|
| 56 |
pdf_content = get_pdf_content()
|
| 57 |
|
| 58 |
-
# Prepare the message with PDF context
|
| 59 |
if pdf_content:
|
| 60 |
messages = [
|
| 61 |
{"role": "user", "parts": [
|
| 62 |
system_prompt,
|
| 63 |
-
types.Part.from_bytes(
|
|
|
|
|
|
|
|
|
|
| 64 |
"Use the PDF content to provide detailed answers about the course."
|
| 65 |
]},
|
| 66 |
*[{"role": "user", "parts": [msg[0]]} for msg in history],
|
|
@@ -80,6 +50,8 @@ def chat(message, history):
|
|
| 80 |
|
| 81 |
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.
|
| 82 |
|
|
|
|
|
|
|
| 83 |
IF USERS ASK ANYTHING NOT RELATED TO COPYXPERT, respond with one of these phrases (vary them creatively):
|
| 84 |
- "¡Ups! Solo hablo de CopyXpert. ¡Es lo único que me apasiona! 🤓"
|
| 85 |
- "¡Beep boop! Error: Pregunta no relacionada con CopyXpert detectada. ¿Hablamos del curso? 🤖"
|
|
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import httpx
|
| 6 |
+
from google.generativeai import types # Add this import
|
| 7 |
|
| 8 |
+
# Update PDF URL
|
| 9 |
+
COURSE_PDF_URL = "https://huggingface.co/spaces/JeCabrera/copywriter2a/resolve/main/Oferta%20CopyXpert.pdf"
|
| 10 |
|
| 11 |
+
# Simplify PDF content retrieval
|
|
|
|
|
|
|
|
|
|
| 12 |
def get_pdf_content():
|
| 13 |
try:
|
| 14 |
+
response = httpx.get(COURSE_PDF_URL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if response.status_code == 200:
|
|
|
|
|
|
|
|
|
|
| 16 |
print(f"PDF Download Status: Success")
|
| 17 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
print(f"Error loading PDF: {e}")
|
| 20 |
return None
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def chat(message, history):
|
| 23 |
try:
|
|
|
|
| 24 |
pdf_content = get_pdf_content()
|
| 25 |
|
|
|
|
| 26 |
if pdf_content:
|
| 27 |
messages = [
|
| 28 |
{"role": "user", "parts": [
|
| 29 |
system_prompt,
|
| 30 |
+
types.Part.from_bytes(
|
| 31 |
+
data=pdf_content,
|
| 32 |
+
mime_type='application/pdf'
|
| 33 |
+
),
|
| 34 |
"Use the PDF content to provide detailed answers about the course."
|
| 35 |
]},
|
| 36 |
*[{"role": "user", "parts": [msg[0]]} for msg in history],
|
|
|
|
| 50 |
|
| 51 |
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.
|
| 52 |
|
| 53 |
+
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.
|
| 54 |
+
|
| 55 |
IF USERS ASK ANYTHING NOT RELATED TO COPYXPERT, respond with one of these phrases (vary them creatively):
|
| 56 |
- "¡Ups! Solo hablo de CopyXpert. ¡Es lo único que me apasiona! 🤓"
|
| 57 |
- "¡Beep boop! Error: Pregunta no relacionada con CopyXpert detectada. ¿Hablamos del curso? 🤖"
|