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