Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import PyPDF2
|
| 3 |
-
from streamlit.runtime.scriptrunner import get_script_run_ctx
|
| 4 |
-
from streamlit.source_util import get_pages
|
| 5 |
import io
|
| 6 |
from typing import List, Dict
|
| 7 |
from groq import Groq
|
|
@@ -13,15 +11,6 @@ st.set_page_config(
|
|
| 13 |
layout="wide"
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# Hide Streamlit menu and footer
|
| 17 |
-
hide_menu_style = """
|
| 18 |
-
<style>
|
| 19 |
-
#MainMenu {visibility: hidden;}
|
| 20 |
-
footer {visibility: hidden;}
|
| 21 |
-
</style>
|
| 22 |
-
"""
|
| 23 |
-
st.markdown(hide_menu_style, unsafe_allow_html=True)
|
| 24 |
-
|
| 25 |
class PDFChatAssistant:
|
| 26 |
def __init__(self):
|
| 27 |
# Initialize session state variables
|
|
@@ -39,7 +28,6 @@ class PDFChatAssistant:
|
|
| 39 |
try:
|
| 40 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 41 |
text = ""
|
| 42 |
-
show_full_text()
|
| 43 |
for page in pdf_reader.pages:
|
| 44 |
text += page.extract_text() + "\n\n"
|
| 45 |
return text
|
|
@@ -51,7 +39,7 @@ class PDFChatAssistant:
|
|
| 51 |
"""
|
| 52 |
Create Groq client with API key
|
| 53 |
"""
|
| 54 |
-
client = Groq(api_key=st.secrets["
|
| 55 |
return client
|
| 56 |
|
| 57 |
def generate_ai_response(self, client: Groq, context: str, user_query: str) -> str:
|
|
@@ -63,7 +51,7 @@ class PDFChatAssistant:
|
|
| 63 |
messages=[
|
| 64 |
{
|
| 65 |
"role": "system",
|
| 66 |
-
"content": f"
|
| 67 |
},
|
| 68 |
{
|
| 69 |
"role": "user",
|
|
@@ -109,37 +97,7 @@ class PDFChatAssistant:
|
|
| 109 |
"role": "user",
|
| 110 |
"content": "Extract the most important key points from this PDF"
|
| 111 |
})
|
| 112 |
-
def show_full_text():
|
| 113 |
-
st.title("📄 Full PDF Text")
|
| 114 |
-
|
| 115 |
-
# Try to retrieve the PDF text from the main page's session state
|
| 116 |
-
ctx = get_script_run_ctx()
|
| 117 |
-
session_info = get_pages('app.py')
|
| 118 |
-
|
| 119 |
-
if 'pdf_text' in st.session_state and st.session_state.pdf_text:
|
| 120 |
-
st.text_area(
|
| 121 |
-
"Complete Extracted Text",
|
| 122 |
-
value=st.session_state.pdf_text,
|
| 123 |
-
height=600,
|
| 124 |
-
disabled=True
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
# Additional buttons
|
| 128 |
-
col1, col2 = st.columns(2)
|
| 129 |
-
with col1:
|
| 130 |
-
if st.button("Copy to Clipboard"):
|
| 131 |
-
st.code(st.session_state.pdf_text)
|
| 132 |
-
st.success("Text copied to clipboard!")
|
| 133 |
-
|
| 134 |
-
with col2:
|
| 135 |
-
if st.button("Back to Chat"):
|
| 136 |
-
st.switch_page("app.py")
|
| 137 |
-
else:
|
| 138 |
-
st.warning("No PDF text available. Please upload a PDF first.")
|
| 139 |
-
if st.button("Back to Chat"):
|
| 140 |
-
st.switch_page("app.py")
|
| 141 |
|
| 142 |
-
|
| 143 |
# Main chat area
|
| 144 |
st.header("Chat with Your PDF")
|
| 145 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import PyPDF2
|
|
|
|
|
|
|
| 3 |
import io
|
| 4 |
from typing import List, Dict
|
| 5 |
from groq import Groq
|
|
|
|
| 11 |
layout="wide"
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
class PDFChatAssistant:
|
| 15 |
def __init__(self):
|
| 16 |
# Initialize session state variables
|
|
|
|
| 28 |
try:
|
| 29 |
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| 30 |
text = ""
|
|
|
|
| 31 |
for page in pdf_reader.pages:
|
| 32 |
text += page.extract_text() + "\n\n"
|
| 33 |
return text
|
|
|
|
| 39 |
"""
|
| 40 |
Create Groq client with API key
|
| 41 |
"""
|
| 42 |
+
client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
| 43 |
return client
|
| 44 |
|
| 45 |
def generate_ai_response(self, client: Groq, context: str, user_query: str) -> str:
|
|
|
|
| 51 |
messages=[
|
| 52 |
{
|
| 53 |
"role": "system",
|
| 54 |
+
"content": f"Bạn là một trợ lý PDF hữu ích. Luôn trả lời bằng tiếng Việt. Sử dụng nội dung trong file PDF đính kèm để trả lời câu hỏi sau:\n\n{context}"
|
| 55 |
},
|
| 56 |
{
|
| 57 |
"role": "user",
|
|
|
|
| 97 |
"role": "user",
|
| 98 |
"content": "Extract the most important key points from this PDF"
|
| 99 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
| 101 |
# Main chat area
|
| 102 |
st.header("Chat with Your PDF")
|
| 103 |
|