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(""" """, 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("Text Craft AI") st.write(""" This application helps you process text by summarizing content, extracting main points """) # 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 @st.cache_data 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('
', 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""" #
#
#
#

{question.strip()}

#
#
#

{answer.strip()}

#
#
#
# """, 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('
', unsafe_allow_html=True) # st.download_button( # label="Download Flashcards", # data=st.session_state.flashcards, # file_name="flashcards.txt", # mime="text/plain" # )