JeCabrera commited on
Commit
705f8a7
·
verified ·
1 Parent(s): c4dc894

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -2
app.py CHANGED
@@ -2,13 +2,28 @@ import os
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
 
5
 
6
  load_dotenv()
7
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
8
 
9
- # Configure model with system prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  model = genai.GenerativeModel(
11
- model_name="gemini-2.0-flash",
12
  generation_config={
13
  "temperature": 0.9,
14
  "top_p": 1,
@@ -16,6 +31,34 @@ model = genai.GenerativeModel(
16
  }
17
  )
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  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.
20
 
21
  IF USERS ASK ANYTHING NOT RELATED TO COPYXPERT, respond with one of these phrases (vary them creatively):
 
2
  import gradio as gr
3
  import google.generativeai as genai
4
  from dotenv import load_dotenv
5
+ import httpx
6
 
7
  load_dotenv()
8
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
9
 
10
+ # PDF URL for course information
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
+ # Convert Google Drive link to direct download link
17
+ file_id = COURSE_PDF_URL.split('/')[5]
18
+ direct_url = f"https://drive.google.com/uc?export=download&id={file_id}"
19
+ return httpx.get(direct_url).content
20
+ except Exception as e:
21
+ print(f"Error loading PDF: {e}")
22
+ return None
23
+
24
+ # Configure model with system prompt and PDF content
25
  model = genai.GenerativeModel(
26
+ model_name="gemini-1.5-flash",
27
  generation_config={
28
  "temperature": 0.9,
29
  "top_p": 1,
 
31
  }
32
  )
33
 
34
+ def chat(message, history):
35
+ try:
36
+ # Get PDF content
37
+ pdf_content = get_pdf_content()
38
+
39
+ # Prepare the message with PDF context
40
+ if pdf_content:
41
+ messages = [
42
+ {"role": "user", "parts": [
43
+ system_prompt,
44
+ types.Part.from_bytes(data=pdf_content, mime_type='application/pdf'),
45
+ "Use the PDF content to provide detailed answers about the course."
46
+ ]},
47
+ *[{"role": "user", "parts": [msg[0]]} for msg in history],
48
+ {"role": "user", "parts": [message]}
49
+ ]
50
+ else:
51
+ messages = [
52
+ {"role": "user", "parts": [system_prompt]},
53
+ *[{"role": "user", "parts": [msg[0]]} for msg in history],
54
+ {"role": "user", "parts": [message]}
55
+ ]
56
+
57
+ response = model.generate_content(messages)
58
+ return response.text
59
+ except Exception as e:
60
+ return f"Error: {e}"
61
+
62
  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.
63
 
64
  IF USERS ASK ANYTHING NOT RELATED TO COPYXPERT, respond with one of these phrases (vary them creatively):