halyn commited on
Commit
80a4c83
β€’
1 Parent(s): 34ef142
Files changed (1) hide show
  1. app.py +87 -85
app.py CHANGED
@@ -3,89 +3,91 @@ 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()
 
3
  from PyPDF2 import PdfReader
4
 
5
 
6
+ st.title("Welcome to GemmaPaperQA")
7
+ st.subheader("Upload Your Paper")
8
+
9
+ # def main_page():
10
+
11
+
12
+ # paper = st.file_uploader("Upload Here!", type="pdf", label_visibility="hidden")
13
+ # if paper:
14
+ # st.write(f"Upload complete! File name is {paper.name}")
15
+ # st.write("Please click the button below.")
16
+ # # pdf_reader = PdfReader(paper)
17
+ # # for page in pdf_reader.pages:
18
+ # # paper_title.append(page.extract_text())
19
+ # # break
20
+ # # paper_name = paper_title[0].split("\n")[0]
21
+
22
+ # # st.subheader(f"You upload the <{paper_name}> paper")
23
+
24
+ # if st.button("Click Here :)"):
25
+ # # FastAPI μ„œλ²„μ— PDF 파일 전솑
26
+ # try:
27
+ # files = {"file": (paper.name, paper, "application/pdf")}
28
+ # response = requests.post(f"{FASTAPI_URL}/upload_pdf", files=files)
29
+ # if response.status_code == 200:
30
+ # st.success("PDF successfully uploaded to the model! Please click the button again")
31
+ # st.session_state.messages = []
32
+ # st.session_state.paper_name = paper.name[:-4]
33
+ # st.session_state.page = "chat"
34
+ # else:
35
+ # st.error(f"Failed to upload PDF to the model. Error: {response.text}")
36
+ # except requests.RequestException as e:
37
+ # st.error(f"Error connecting to the server: {str(e)}")
38
+
39
+ # def chat_page():
40
+ # st.title(f"Welcome to GemmaPaperQA")
41
+ # st.subheader(f"Ask anything about {st.session_state.paper_name}")
42
+
43
+ # if "messages" not in st.session_state:
44
+ # st.session_state.messages = []
45
+
46
+ # for message in st.session_state.messages:
47
+ # with st.chat_message(message["role"]):
48
+ # st.markdown(message["content"])
49
 
50
+ # if prompt := st.chat_input("Chat here !"):
51
+ # # Add user message to chat history
52
+ # st.session_state.messages.append({"role": "user", "content": prompt})
53
+
54
+ # # Display user message in chat message container
55
+ # with st.chat_message("user"):
56
+ # st.markdown(prompt)
57
+
58
+ # # Get response from FastAPI server
59
+ # response = get_response_from_fastapi(prompt)
60
+
61
+ # # Display assistant response in chat message container
62
+ # with st.chat_message("assistant"):
63
+ # st.markdown(response)
64
+
65
+ # # Add assistant response to chat history
66
+ # st.session_state.messages.append({"role": "assistant", "content": response})
67
+
68
+ # if st.button("Go back to main page"):
69
+ # st.session_state.page = "main"
70
+
71
+ # def get_response_from_fastapi(prompt):
72
+ # try:
73
+ # response = requests.post(f"{FASTAPI_URL}/ask", json={"text": prompt})
74
+ # if response.status_code == 200:
75
+ # return response.json()["response"]
76
+ # else:
77
+ # return f"Sorry, I couldn't generate a response. Error: {response.text}"
78
+ # except requests.RequestException as e:
79
+ # return f"Sorry, there was an error connecting to the server: {str(e)}"
80
+
81
+ # # 초기 νŽ˜μ΄μ§€ μ„€μ •
82
+ # if "page" not in st.session_state:
83
+ # st.session_state.page = "main"
84
+
85
+ # # paper_name μ΄ˆκΈ°ν™”
86
+ # if "paper_name" not in st.session_state:
87
+ # st.session_state.paper_name = ""
88
+
89
+ # # νŽ˜μ΄μ§€ λ Œλ”λ§
90
+ # if st.session_state.page == "main":
91
+ # main_page()
92
+ # elif st.session_state.page == "chat":
93
+ # chat_page()