Spaces:
Sleeping
Sleeping
import streamlit as st | |
from config import get_ai71_api_key | |
from ai_utils import generate_response as _generate_response | |
# Set the page configuration to wide layout | |
st.set_page_config(layout="wide") | |
# Custom CSS to style the page, text area, and flashcards | |
st.markdown(""" | |
<style> | |
.main .block-container { | |
padding: 2rem; | |
} | |
.title { | |
text-align: center; | |
margin-bottom: 1rem; | |
} | |
.section-title { | |
margin-top: 2rem; | |
font-size: 1.5rem; | |
} | |
.text-area { | |
margin-top: 1rem; | |
margin-bottom: 1rem; | |
} | |
.flashcard-container { | |
display: flex; | |
overflow-x: auto; /* Allow horizontal scrolling */ | |
white-space: nowrap; /* Prevent wrapping */ | |
gap: 1rem; | |
} | |
.flashcard { | |
width: 250px; | |
height: 200px; | |
border: 2px solid #4682b4; /* Steel Blue border */ | |
border-radius: 5px; | |
font-size: 16px; | |
color: black; | |
background-color: #f0f8ff; /* Alice Blue background */ | |
perspective: 1000px; | |
cursor: pointer; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
flex-shrink: 0; /* Prevent shrinking */ | |
} | |
.flashcard-inner { | |
position: relative; | |
width: 100%; | |
height: 100%; | |
transition: transform 0.6s; | |
transform-style: preserve-3d; | |
} | |
.flashcard:hover .flashcard-inner { | |
transform: rotateY(180deg); | |
} | |
.flashcard-front, .flashcard-back { | |
position: absolute; | |
width: 100%; | |
height: 100%; | |
backface-visibility: hidden; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
padding: 1rem; | |
} | |
.flashcard-front { | |
background-color: #f0f8ff; /* Alice Blue background */ | |
} | |
.flashcard-back { | |
background-color: #ffffff; /* White background for the back */ | |
transform: rotateY(180deg); | |
} | |
</style> | |
""", unsafe_allow_html=True) | |
# Initialize session state variables if not already set | |
if 'input_text' not in st.session_state: | |
st.session_state.input_text = "" | |
if 'summary' not in st.session_state: | |
st.session_state.summary = "" | |
if 'main_points' not in st.session_state: | |
st.session_state.main_points = "" | |
if 'flashcards' not in st.session_state: | |
st.session_state.flashcards = "" | |
# Display the title and introduction | |
st.title("InsightStarfleet") | |
st.write(""" | |
Welcome to the InsightStarfleet! This application helps you process text by summarizing content, | |
extracting main points, and creating flashcards. Follow the instructions to enter your text and generate content. | |
""") | |
# Input area for text | |
st.subheader("Enter Your Text") | |
input_text = st.text_area( | |
label="Enter text below:", | |
value=st.session_state.input_text, | |
height=200, | |
key="input_text", | |
help="Paste or type your text here for processing." | |
) | |
# Update session state if input text changes | |
if input_text != st.session_state.input_text: | |
st.session_state.input_text = input_text | |
# Text Processing | |
st.subheader("Text Processing") | |
if st.session_state.input_text: | |
# Caching API call function | |
def generate_response(system_message, user_message): | |
response = _generate_response(system_message, user_message) | |
# Strip any unwanted labels or text | |
return response.strip() | |
# Buttons for processing | |
if st.button("Generate Summary"): | |
summary_prompt = "Summarize the following text into a brief summary:" | |
st.session_state.summary = generate_response(summary_prompt, st.session_state.input_text) | |
if st.button("Extract Main Points"): | |
main_points_prompt = "Extract the main points from the following text and format them as a bulleted list:" | |
st.session_state.main_points = generate_response(main_points_prompt, st.session_state.input_text) | |
if st.button("Generate Flashcards"): | |
flashcards_prompt = "Create flashcards with questions and answers based on the following text. Ensure each flashcard includes a question and its corresponding answer, formatted as 'Question: [question]\\nAnswer: [answer]':" | |
st.session_state.flashcards = generate_response(flashcards_prompt, st.session_state.input_text) | |
else: | |
# Only show generate buttons if text is present | |
if st.button("Generate Summary") or st.button("Extract Main Points") or st.button("Generate Flashcards"): | |
st.error("Please enter text before generating content.") | |
# Combined Results | |
st.subheader("Results") | |
# Summary | |
if st.session_state.summary: | |
st.markdown("### Summary") | |
st.text_area( | |
"Summary", | |
value=st.session_state.summary, | |
height=200, | |
key="summary_results", | |
help="Generated summary." | |
) | |
st.download_button( | |
label="Download Summary", | |
data=st.session_state.summary, | |
file_name="summary.txt", | |
mime="text/plain" | |
) | |
# Main Points | |
if st.session_state.main_points: | |
st.markdown("### Main Points") | |
# Remove any leading or trailing blank lines from main points | |
bullet_points = "\n".join([f"- {point.strip()}" for point in st.session_state.main_points.split("\n") if point.strip()]) | |
st.markdown(f"### Main Points\n{bullet_points}", unsafe_allow_html=True) | |
st.download_button( | |
label="Download Main Points", | |
data=bullet_points, | |
file_name="main_points.txt", | |
mime="text/plain" | |
) | |
# Flashcards | |
if st.session_state.flashcards: | |
st.markdown("### Flashcards") | |
flashcards = st.session_state.flashcards.split("\n\n") # Assuming flashcards are separated by double newlines | |
st.markdown('<div class="flashcard-container">', unsafe_allow_html=True) | |
for card in flashcards: | |
try: | |
# Check if card contains both question and answer | |
if '\n' in card: | |
question, answer = card.split('\n', 1) | |
st.markdown(f""" | |
<div class="flashcard"> | |
<div class="flashcard-inner"> | |
<div class="flashcard-front"> | |
<p>{question.strip()}</p> | |
</div> | |
<div class="flashcard-back"> | |
<p>{answer.strip()}</p> | |
</div> | |
</div> | |
</div> | |
""", unsafe_allow_html=True) | |
else: | |
st.error("Invalid flashcard format. Each flashcard should be in 'question\\nanswer' format.") | |
except Exception as e: | |
st.error(f"Error processing flashcard: {e}") | |
st.markdown('</div>', unsafe_allow_html=True) | |
st.download_button( | |
label="Download Flashcards", | |
data=st.session_state.flashcards, | |
file_name="flashcards.txt", | |
mime="text/plain" | |
) | |