Spaces:
Paused
Paused
Add application file
Browse files- app.py +89 -2
- requirements.txt +3 -0
app.py
CHANGED
@@ -1,4 +1,91 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
|
5 |
+
|
6 |
+
def main_page():
|
7 |
+
st.title("Welcome to GemmaPaperQA")
|
8 |
+
st.subheader("Upload Your Paper")
|
9 |
+
|
10 |
+
paper = st.file_uploader("Upload Here!", type="pdf", label_visibility="hidden")
|
11 |
+
if paper:
|
12 |
+
st.write(f"Upload complete! File name is {paper.name}")
|
13 |
+
st.write("Please click the button below.")
|
14 |
+
# pdf_reader = PdfReader(paper)
|
15 |
+
# for page in pdf_reader.pages:
|
16 |
+
# paper_title.append(page.extract_text())
|
17 |
+
# break
|
18 |
+
# paper_name = paper_title[0].split("\n")[0]
|
19 |
+
|
20 |
+
# st.subheader(f"You upload the <{paper_name}> paper")
|
21 |
+
|
22 |
+
if st.button("Click Here :)"):
|
23 |
+
# FastAPI ์๋ฒ์ PDF ํ์ผ ์ ์ก
|
24 |
+
try:
|
25 |
+
files = {"file": (paper.name, paper, "application/pdf")}
|
26 |
+
response = requests.post(f"{FASTAPI_URL}/upload_pdf", files=files)
|
27 |
+
if response.status_code == 200:
|
28 |
+
st.success("PDF successfully uploaded to the model! Please click the button again")
|
29 |
+
st.session_state.messages = []
|
30 |
+
st.session_state.paper_name = paper.name[:-4]
|
31 |
+
st.session_state.page = "chat"
|
32 |
+
else:
|
33 |
+
st.error(f"Failed to upload PDF to the model. Error: {response.text}")
|
34 |
+
except requests.RequestException as e:
|
35 |
+
st.error(f"Error connecting to the server: {str(e)}")
|
36 |
+
|
37 |
+
def chat_page():
|
38 |
+
st.title(f"Welcome to GemmaPaperQA")
|
39 |
+
st.subheader(f"Ask anything about {st.session_state.paper_name}")
|
40 |
+
|
41 |
+
if "messages" not in st.session_state:
|
42 |
+
st.session_state.messages = []
|
43 |
+
|
44 |
+
for message in st.session_state.messages:
|
45 |
+
with st.chat_message(message["role"]):
|
46 |
+
st.markdown(message["content"])
|
47 |
+
|
48 |
+
if prompt := st.chat_input("Chat here !"):
|
49 |
+
# Add user message to chat history
|
50 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
51 |
+
|
52 |
+
# Display user message in chat message container
|
53 |
+
with st.chat_message("user"):
|
54 |
+
st.markdown(prompt)
|
55 |
+
|
56 |
+
# Get response from FastAPI server
|
57 |
+
response = get_response_from_fastapi(prompt)
|
58 |
+
|
59 |
+
# Display assistant response in chat message container
|
60 |
+
with st.chat_message("assistant"):
|
61 |
+
st.markdown(response)
|
62 |
+
|
63 |
+
# Add assistant response to chat history
|
64 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
65 |
+
|
66 |
+
if st.button("Go back to main page"):
|
67 |
+
st.session_state.page = "main"
|
68 |
+
|
69 |
+
def get_response_from_fastapi(prompt):
|
70 |
+
try:
|
71 |
+
response = requests.post(f"{FASTAPI_URL}/ask", json={"text": prompt})
|
72 |
+
if response.status_code == 200:
|
73 |
+
return response.json()["response"]
|
74 |
+
else:
|
75 |
+
return f"Sorry, I couldn't generate a response. Error: {response.text}"
|
76 |
+
except requests.RequestException as e:
|
77 |
+
return f"Sorry, there was an error connecting to the server: {str(e)}"
|
78 |
+
|
79 |
+
# ์ด๊ธฐ ํ์ด์ง ์ค์
|
80 |
+
if "page" not in st.session_state:
|
81 |
+
st.session_state.page = "main"
|
82 |
+
|
83 |
+
# paper_name ์ด๊ธฐํ
|
84 |
+
if "paper_name" not in st.session_state:
|
85 |
+
st.session_state.paper_name = ""
|
86 |
+
|
87 |
+
# ํ์ด์ง ๋ ๋๋ง
|
88 |
+
if st.session_state.page == "main":
|
89 |
+
main_page()
|
90 |
+
elif st.session_state.page == "chat":
|
91 |
+
chat_page()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
requests
|
3 |
+
PyPDF2
|