Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -34,26 +34,24 @@ def chat(message, history):
|
|
34 |
try:
|
35 |
pdf_content = get_pdf_content()
|
36 |
|
|
|
|
|
|
|
|
|
37 |
if pdf_content:
|
38 |
-
|
39 |
{"role": "user", "parts": [
|
40 |
system_prompt,
|
41 |
-
types.Part.from_bytes(
|
42 |
-
data=pdf_content,
|
43 |
-
mime_type='application/pdf'
|
44 |
-
),
|
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 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
response = model.generate_content(messages)
|
58 |
return response.text
|
59 |
except Exception as e:
|
|
|
34 |
try:
|
35 |
pdf_content = get_pdf_content()
|
36 |
|
37 |
+
# Prepare history with previous messages
|
38 |
+
history_messages = [{"role": "user", "parts": [msg[0]]} for msg in history]
|
39 |
+
|
40 |
+
# Create base message with system prompt and PDF if available
|
41 |
if pdf_content:
|
42 |
+
base_message = [
|
43 |
{"role": "user", "parts": [
|
44 |
system_prompt,
|
45 |
+
types.Part.from_bytes(data=pdf_content, mime_type='application/pdf'),
|
|
|
|
|
|
|
46 |
"Use the PDF content to provide detailed answers about the course."
|
47 |
+
]}
|
|
|
|
|
48 |
]
|
49 |
else:
|
50 |
+
base_message = [{"role": "user", "parts": [system_prompt]}]
|
51 |
+
|
52 |
+
# Combine all messages
|
53 |
+
messages = base_message + history_messages + [{"role": "user", "parts": [message]}]
|
54 |
+
|
|
|
55 |
response = model.generate_content(messages)
|
56 |
return response.text
|
57 |
except Exception as e:
|