Spaces:
Paused
Paused
modify 2 page to 1 main page
Browse files
app.py
CHANGED
|
@@ -49,93 +49,74 @@ def load_model():
|
|
| 49 |
print(f"Error loading model: {e}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
def
|
| 54 |
-
global qa_chain
|
| 55 |
-
try:
|
| 56 |
-
pipe = load_model()
|
| 57 |
-
except Exception as e:
|
| 58 |
-
print(f"Error loading model: {e}")
|
| 59 |
-
return
|
| 60 |
-
llm = HuggingFacePipeline(pipeline=pipe)
|
| 61 |
-
qa_chain = load_qa_chain(llm, chain_type="map_rerank")
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# st.session_state.paper_name = paper.name[:-4]
|
| 65 |
-
st.session_state.page = "chat"
|
| 66 |
-
st.success("PDF successfully processed! You can now ask questions.")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# ๋ฉ์ธ ํ์ด์ง UI
|
| 70 |
-
def main_page():
|
| 71 |
st.title("Welcome to GemmaPaperQA")
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
st.write(f"Total characters extracted: {len(text)}")
|
| 98 |
-
global knowledge_base
|
| 99 |
-
if st.button("Proceed with this file"):
|
| 100 |
-
chunks = split_text(text)
|
| 101 |
-
knowledge_base = create_knowledge_base(chunks)
|
| 102 |
-
|
| 103 |
-
if knowledge_base is None:
|
| 104 |
-
st.error("Failed to create knowledge base.")
|
| 105 |
return
|
| 106 |
-
|
| 107 |
-
setup_qa_chain()
|
| 108 |
-
|
| 109 |
-
except Exception as e:
|
| 110 |
-
st.error(f"Failed to process the PDF: {str(e)}")
|
| 111 |
-
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# ๋ชจ๋ธ ์๋ต ์ฒ๋ฆฌ
|
| 141 |
def get_response_from_model(prompt):
|
|
@@ -156,15 +137,7 @@ def get_response_from_model(prompt):
|
|
| 156 |
except Exception as e:
|
| 157 |
return f"Error: {str(e)}"
|
| 158 |
|
| 159 |
-
# ํ์ด์ง ์ค์
|
| 160 |
-
if "page" not in st.session_state:
|
| 161 |
-
st.session_state.page = "main"
|
| 162 |
-
|
| 163 |
-
if "paper_name" not in st.session_state:
|
| 164 |
-
st.session_state.paper_name = ""
|
| 165 |
|
| 166 |
-
#
|
| 167 |
-
if
|
| 168 |
-
|
| 169 |
-
elif st.session_state.page == "chat":
|
| 170 |
-
chat_page()
|
|
|
|
| 49 |
print(f"Error loading model: {e}")
|
| 50 |
return None
|
| 51 |
|
| 52 |
+
# ํ์ด์ง UI
|
| 53 |
+
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
st.title("Welcome to GemmaPaperQA")
|
| 55 |
+
|
| 56 |
+
# PDF ์
๋ก๋ ์น์
|
| 57 |
+
with st.expander("Upload Your Paper", expanded=True):
|
| 58 |
+
paper = st.file_uploader("Upload Here!", type="pdf", label_visibility="hidden")
|
| 59 |
+
|
| 60 |
+
if paper:
|
| 61 |
+
st.write(f"Upload complete! File name: {paper.name}")
|
| 62 |
+
|
| 63 |
+
# ํ์ผ ํฌ๊ธฐ ํ์ธ
|
| 64 |
+
file_size = paper.size # ํ์ผ ํฌ๊ธฐ๋ฅผ ํ์ผ ํฌ์ธํฐ ์ด๋ ์์ด ํ์ธ
|
| 65 |
+
if file_size > 10 * 1024 * 1024: # 10MB ์ ํ
|
| 66 |
+
st.error("File is too large! Please upload a file smaller than 10MB.")
|
| 67 |
+
return
|
| 68 |
+
|
| 69 |
+
# PDF ํ
์คํธ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
|
| 70 |
+
with st.spinner('Processing PDF...'):
|
| 71 |
+
try:
|
| 72 |
+
paper.seek(0)
|
| 73 |
+
contents = paper.read()
|
| 74 |
+
pdf_file = io.BytesIO(contents)
|
| 75 |
+
text = load_pdf(pdf_file)
|
| 76 |
+
print("text:", text)
|
| 77 |
+
|
| 78 |
+
if len(text.strip()) == 0:
|
| 79 |
+
st.error("The PDF appears to have no extractable text. Please check the file and try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
st.text_area("Preview of extracted text", text[:1000], height=200)
|
| 83 |
+
st.write(f"Total characters extracted: {len(text)}")
|
| 84 |
+
|
| 85 |
+
if st.button("Create Knowledge Base"):
|
| 86 |
+
global knowledge_base
|
| 87 |
+
chunks = split_text(text)
|
| 88 |
+
print("chunks:", chunks)
|
| 89 |
+
knowledge_base = create_knowledge_base(chunks)
|
| 90 |
+
print("knowledge_base:", knowledge_base)
|
| 91 |
+
|
| 92 |
+
if knowledge_base is None:
|
| 93 |
+
st.error("Failed to create knowledge base.")
|
| 94 |
+
return
|
| 95 |
+
|
| 96 |
+
# QA ์ฒด์ธ ์ค์
|
| 97 |
+
global qa_chain
|
| 98 |
+
try:
|
| 99 |
+
pipe = load_model()
|
| 100 |
+
except Exception as e:
|
| 101 |
+
st.error(f"Error loading model: {e}")
|
| 102 |
+
return
|
| 103 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
| 104 |
+
qa_chain = load_qa_chain(llm, chain_type="map_rerank")
|
| 105 |
+
|
| 106 |
+
st.success("Knowledge base created! You can now ask questions.")
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"Failed to process the PDF: {str(e)}")
|
| 110 |
+
|
| 111 |
+
# ์ง๋ฌธ-์๋ต ์น์
|
| 112 |
+
if knowledge_base and qa_chain:
|
| 113 |
+
with st.expander("Ask Questions", expanded=True):
|
| 114 |
+
prompt = st.text_input("Chat here!")
|
| 115 |
+
|
| 116 |
+
if prompt:
|
| 117 |
+
response = get_response_from_model(prompt)
|
| 118 |
+
if response:
|
| 119 |
+
st.write(f"**Assistant**: {response}")
|
| 120 |
|
| 121 |
# ๋ชจ๋ธ ์๋ต ์ฒ๋ฆฌ
|
| 122 |
def get_response_from_model(prompt):
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
return f"Error: {str(e)}"
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# ์ฑ ์คํ
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
main()
|
|
|
|
|
|