Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import PyPDF2
|
4 |
+
from groq import Groq
|
5 |
+
|
6 |
+
# Set up your Groq client
|
7 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
8 |
+
|
9 |
+
# Function to extract text from a PDF
|
10 |
+
def extract_text_from_pdf(pdf_file):
|
11 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
12 |
+
text = ""
|
13 |
+
for page in pdf_reader.pages:
|
14 |
+
text += page.extract_text()
|
15 |
+
return text
|
16 |
+
|
17 |
+
# Function to query Groq for simplified content or Q&A
|
18 |
+
def query_groq(prompt):
|
19 |
+
chat_completion = client.chat.completions.create(
|
20 |
+
messages=[
|
21 |
+
{
|
22 |
+
"role": "user",
|
23 |
+
"content": prompt,
|
24 |
+
}
|
25 |
+
],
|
26 |
+
model="llama-3.3-70b-versatile",
|
27 |
+
)
|
28 |
+
return chat_completion.choices[0].message.content
|
29 |
+
|
30 |
+
# Persistent history
|
31 |
+
if "history" not in st.session_state:
|
32 |
+
st.session_state["history"] = []
|
33 |
+
|
34 |
+
# Streamlit App
|
35 |
+
st.set_page_config(
|
36 |
+
page_title="π Next-Gen Scientific Paper Translator",
|
37 |
+
layout="wide",
|
38 |
+
initial_sidebar_state="expanded"
|
39 |
+
)
|
40 |
+
|
41 |
+
# Apply custom CSS for animations and background
|
42 |
+
st.markdown(
|
43 |
+
"""
|
44 |
+
<style>
|
45 |
+
body {
|
46 |
+
background-image: url('https://source.unsplash.com/1920x1080/?science,technology');
|
47 |
+
background-size: cover;
|
48 |
+
background-attachment: fixed;
|
49 |
+
color: #FFFFFF;
|
50 |
+
}
|
51 |
+
.css-18e3th9 {
|
52 |
+
background: rgba(0, 0, 0, 0.7) !important;
|
53 |
+
border-radius: 10px;
|
54 |
+
padding: 20px;
|
55 |
+
}
|
56 |
+
button {
|
57 |
+
transition: 0.3s ease;
|
58 |
+
}
|
59 |
+
button:hover {
|
60 |
+
background-color: #1f78d1 !important;
|
61 |
+
color: white !important;
|
62 |
+
}
|
63 |
+
</style>
|
64 |
+
""",
|
65 |
+
unsafe_allow_html=True,
|
66 |
+
)
|
67 |
+
|
68 |
+
st.title("π **Next-Gen Scientific Paper Translator**")
|
69 |
+
st.markdown(
|
70 |
+
"Unlock the power of **scientific papers** with simplified explanations, extracted citations, and more!"
|
71 |
+
)
|
72 |
+
|
73 |
+
# Sidebar with user options
|
74 |
+
with st.sidebar:
|
75 |
+
st.markdown("## βοΈ **Settings**")
|
76 |
+
theme_mode = st.radio("Choose Theme", ["Light Mode", "Dark Mode"])
|
77 |
+
if theme_mode == "Dark Mode":
|
78 |
+
st.markdown("""
|
79 |
+
<style>
|
80 |
+
body {
|
81 |
+
background-color: #1E1E1E;
|
82 |
+
}
|
83 |
+
</style>
|
84 |
+
""", unsafe_allow_html=True)
|
85 |
+
|
86 |
+
# Upload PDF
|
87 |
+
st.markdown("### π€ **Upload Your PDF**")
|
88 |
+
pdf_file = st.file_uploader("Drop your scientific paper (PDF only)", type=["pdf"])
|
89 |
+
|
90 |
+
# Left column for preview, right column for output
|
91 |
+
if pdf_file is not None:
|
92 |
+
col1, col2 = st.columns([1, 2])
|
93 |
+
|
94 |
+
# PDF Preview
|
95 |
+
with col1:
|
96 |
+
st.markdown("### π **PDF Preview**")
|
97 |
+
text = extract_text_from_pdf(pdf_file)
|
98 |
+
st.text_area("Preview of Content", text[:1500], height=400)
|
99 |
+
|
100 |
+
# Right column: Options for processing
|
101 |
+
with col2:
|
102 |
+
st.markdown("### π§ **What would you like to do?**")
|
103 |
+
task = st.radio(
|
104 |
+
"Select a task",
|
105 |
+
["Simplify the Content", "Extract Title", "Extract Citation", "Ask a Question"]
|
106 |
+
)
|
107 |
+
|
108 |
+
if task == "Simplify the Content":
|
109 |
+
prompt = f"Simplify this for a beginner:\n\n{text[:1000]}"
|
110 |
+
if st.button("Simplify"):
|
111 |
+
with st.spinner("Processing..."):
|
112 |
+
response = query_groq(prompt)
|
113 |
+
st.session_state["history"].append({"task": "Simplify", "output": response})
|
114 |
+
st.success("Simplified!")
|
115 |
+
st.write(response)
|
116 |
+
|
117 |
+
elif task == "Extract Title":
|
118 |
+
prompt = f"Extract the title:\n\n{text[:1000]}"
|
119 |
+
if st.button("Extract Title"):
|
120 |
+
with st.spinner("Processing..."):
|
121 |
+
response = query_groq(prompt)
|
122 |
+
st.session_state["history"].append({"task": "Title", "output": response})
|
123 |
+
st.success("Title extracted!")
|
124 |
+
st.write(response)
|
125 |
+
|
126 |
+
elif task == "Extract Citation":
|
127 |
+
prompt = f"Extract the citation:\n\n{text[:1000]}"
|
128 |
+
if st.button("Extract Citation"):
|
129 |
+
with st.spinner("Processing..."):
|
130 |
+
response = query_groq(prompt)
|
131 |
+
st.session_state["history"].append({"task": "Citation", "output": response})
|
132 |
+
st.success("Citation extracted!")
|
133 |
+
st.write(response)
|
134 |
+
|
135 |
+
elif task == "Ask a Question":
|
136 |
+
user_question = st.text_input("Enter your question:")
|
137 |
+
if st.button("Get Answer"):
|
138 |
+
prompt = f"Answer this question:\n\n{text[:1000]}\n\nQuestion: {user_question}"
|
139 |
+
with st.spinner("Processing..."):
|
140 |
+
response = query_groq(prompt)
|
141 |
+
st.session_state["history"].append({"task": "Q&A", "output": response})
|
142 |
+
st.success("Answer generated!")
|
143 |
+
st.write(response)
|
144 |
+
|
145 |
+
# History Section
|
146 |
+
st.markdown("---")
|
147 |
+
st.markdown("### π **History**")
|
148 |
+
for entry in st.session_state["history"]:
|
149 |
+
st.markdown(f"**Task:** {entry['task']} | **Output:** {entry['output']}")
|
150 |
+
|
151 |
+
# Footer
|
152 |
+
st.markdown("---")
|
153 |
+
st.markdown(
|
154 |
+
"<div style='text-align: center;'>Built with β€οΈ using Streamlit and Groq</div>",
|
155 |
+
unsafe_allow_html=True,
|
156 |
+
)
|