Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import streamlit as stimport pandas as pdfrom openai_llms import LLMHandlerimport tempfileimport os# Load environment variables for OpenAI API Keyfrom dotenv import load_dotenvload_dotenv()# Initialize LLMHandlerllm_handler = LLMHandler()def process_csv(file, user_prompt):"""Read CSV, generate responses using LLMHandler, and return processed DataFrame."""df = pd.read_csv(file)responses = []for _, row in df.iterrows():try:response = llm_handler.generate_response(user_prompt, row.to_dict())responses.append(response)except Exception as e:responses.append(f"Error: {e}")df["Generated Text"] = responsesreturn df# Streamlit UIst.set_page_config(page_title="Invitation Generator", page_icon="π", layout="wide")# Headerst.title("Invite AI")st.markdown("""Welcome to the Invitation Generator! This tool helps you create personalized invitations using the power of AI.Follow the steps below to upload your data and generate professional invitation texts.""")# Section: Template Download and Instructionsst.sidebar.title("Instructions")st.sidebar.markdown("""### Template Download[Click here to download the suggested CSV template](http://surl.li/ptvzzv) π₯### Required Fields- **Unique Identifier for each receiver**- **Name of the receiver**- **Designation/Job title of the receiver**- **Company/Organisation where the receiver works**- **Areas the receiver is interested in / has expertise in**- **Categorize receivers into groups** for consistent instructions.""")# Main Sectionst.markdown("""### Steps to Use1. **Download the template** from the sidebar.2. **Fill out the details** as per the instructions.3. **Upload the completed CSV file** below.4. Enter a **prompt** to generate personalized invitations.""")# File uploader for CSVuploaded_file = st.file_uploader("π Upload CSV File", type=["csv"])user_prompt = st.text_area("βοΈ Enter the prompt for generating invitation texts:","Write professional invitation text tailored for the group.",height=150,)# Processing and displaying resultsif uploaded_file is not None and user_prompt:st.write("β³ Processing your file... Please wait.")processed_df = process_csv(uploaded_file, user_prompt)# Display resultsst.write("### Generated Invitations")st.dataframe(processed_df, use_container_width=True)# Option to download the processed CSVwith tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:processed_df.to_csv(temp_file.name, index=False)temp_file.close() # Ensure the file is properly closed before proceedingst.download_button(label="π₯ Download Results CSV",data=open(temp_file.name, "rb"),file_name="generated_invitations.csv",mime="text/csv",)# Safely delete the temporary fileos.unlink(temp_file.name)# Footerst.markdown("---")st.markdown("π‘ **Tip:** Ensure your data aligns with the provided template for accurate results.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import streamlit as stimport pandas as pd#from openai_llms import LLMHandlerfrom Groq_llms import LLMHandlerimport tempfileimport os# Load environment variables for OpenAI API Keyfrom dotenv import load_dotenvload_dotenv()# Initialize LLMHandlerllm_handler = LLMHandler()def process_csv(file, user_prompt):"""Read CSV, generate responses using LLMHandler, and return processed DataFrame."""df = pd.read_csv(file)responses = []for _, row in df.iterrows():try:response = llm_handler.generate_response(user_prompt, row.to_dict())responses.append(response)except Exception as e:responses.append(f"Error: {e}")df["Generated Text"] = responsesreturn df# Streamlit UIst.set_page_config(page_title="Invitation Generator", page_icon="π¬", layout="wide")# Headerst.title("Invite AI")st.markdown("""Welcome to the Invitation Generator! This tool helps you create personalized invitations using the power of AI.Follow the steps below to upload your data and generate professional invitation texts.""")# Section: Template Download and Instructionsst.sidebar.title("Instructions")st.sidebar.markdown("""### Template Download[Click here to download the suggested CSV template](http://surl.li/ptvzzv) π₯### Required Fields- **Unique Identifier for each receiver**- **Name of the receiver**- **Designation/Job title of the receiver**- **Company/Organisation where the receiver works**- **Areas the receiver is interested in / has expertise in**- **Categorize receivers into groups** for consistent instructions.""")# Main Sectionst.markdown("""### Steps to Use1. **Download the template** from the sidebar.2. **Fill out the details** as per the instructions.3. **Upload the completed CSV file** below.4. Enter a **prompt** to generate personalized invitations.""")# File uploader for CSVuploaded_file = st.file_uploader("π Upload CSV File", type=["csv"])user_prompt = st.text_area("βοΈ Enter the prompt for generating invitation texts:","Write professional invitation text tailored for the group.",height=150,)# Processing and displaying resultsif uploaded_file is not None and user_prompt:st.write("β³ Processing your file... Please wait.")processed_df = process_csv(uploaded_file, user_prompt)# Display resultsst.write("### Generated Invitations")st.dataframe(processed_df, use_container_width=True)# Option to download the processed CSVwith tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:processed_df.to_csv(temp_file.name, index=False)temp_file.close() # Ensure the file is properly closed before proceedingst.download_button(label="π₯ Download Results CSV",data=open(temp_file.name, "rb"),file_name="generated_invitations.csv",mime="text/csv",)# Safely delete the temporary fileos.unlink(temp_file.name)# Footerst.markdown("---")st.markdown("π‘ **Tip:** Ensure your data aligns with the provided template for accurate results.")
app.py+109-108