Spaces:
Sleeping
Sleeping
Update app.py
Browse files112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710823456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import streamlit as stimport streamlit as st
import pandas as pd
from openai_llms import LLMHandler
import tempfile
import os
# Load environment variables for OpenAI API Key
from dotenv import load_dotenv
load_dotenv()
# Initialize LLMHandler
llm_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"] = responses
return df
# Streamlit UI
st.set_page_config(page_title="Invitation Generator", page_icon="π", layout="wide")
# Header
st.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 Instructions
st.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 Section
st.markdown(
"""
### Steps to Use
1. **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 CSV
uploaded_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 results
if uploaded_file is not None and user_prompt:
st.write("β³ Processing your file... Please wait.")
processed_df = process_csv(uploaded_file, user_prompt)
# Display results
st.write("### Generated Invitations")
st.dataframe(processed_df, use_container_width=True)
# Option to download the processed CSV
with 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 proceeding
st.download_button(
label="π₯ Download Results CSV",
data=open(temp_file.name, "rb"),
file_name="generated_invitations.csv",
mime="text/csv",
)
# Safely delete the temporary file
os.unlink(temp_file.name)
# Footer
st.markdown("---")
st.markdown(
"π‘ **Tip:** Ensure your data aligns with the provided template for accurate results."
)import pandas as pd#from openai_llms import LLMHandlerfrom Groq_llms import LLMHandlerimport tempfileimport os
# Load environment variables for OpenAI API Keyfrom dotenv import load_dotenv
load_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"] = responses return 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 Use 1. **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 results st.write("### Generated Invitations") st.dataframe(processed_df, use_container_width=True)
# Option to download the processed CSV with 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 proceeding
st.download_button( label="π₯ Download Results CSV", data=open(temp_file.name, "rb"), file_name="generated_invitations.csv", mime="text/csv", )
# Safely delete the temporary file os.unlink(temp_file.name)
# Footerst.markdown("---")st.markdown( "π‘ **Tip:** Ensure your data aligns with the provided template for accurate results.")app.py+109-108