Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,109 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from openai_llms import LLMHandler
|
| 4 |
-
import
|
| 5 |
-
import
|
| 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 |
-
st.
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.sidebar.
|
| 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 |
-
st.
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
temp_file.
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
st.markdown(
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
#from openai_llms import LLMHandler
|
| 4 |
+
from Groq_llms import LLMHandler
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load environment variables for OpenAI API Key
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Initialize LLMHandler
|
| 14 |
+
llm_handler = LLMHandler()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def process_csv(file, user_prompt):
|
| 18 |
+
"""Read CSV, generate responses using LLMHandler, and return processed DataFrame."""
|
| 19 |
+
df = pd.read_csv(file)
|
| 20 |
+
responses = []
|
| 21 |
+
|
| 22 |
+
for _, row in df.iterrows():
|
| 23 |
+
try:
|
| 24 |
+
response = llm_handler.generate_response(user_prompt, row.to_dict())
|
| 25 |
+
responses.append(response)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
responses.append(f"Error: {e}")
|
| 28 |
+
|
| 29 |
+
df["Generated Text"] = responses
|
| 30 |
+
return df
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# Streamlit UI
|
| 34 |
+
st.set_page_config(page_title="Invitation Generator", page_icon="π¬", layout="wide")
|
| 35 |
+
|
| 36 |
+
# Header
|
| 37 |
+
st.title("Invite AI")
|
| 38 |
+
st.markdown(
|
| 39 |
+
"""
|
| 40 |
+
Welcome to the Invitation Generator! This tool helps you create personalized invitations using the power of AI.
|
| 41 |
+
Follow the steps below to upload your data and generate professional invitation texts.
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# Section: Template Download and Instructions
|
| 46 |
+
st.sidebar.title("Instructions")
|
| 47 |
+
st.sidebar.markdown(
|
| 48 |
+
"""
|
| 49 |
+
### Template Download
|
| 50 |
+
[Click here to download the suggested CSV template](http://surl.li/ptvzzv) π₯
|
| 51 |
+
|
| 52 |
+
### Required Fields
|
| 53 |
+
- **Unique Identifier for each receiver**
|
| 54 |
+
- **Name of the receiver**
|
| 55 |
+
- **Designation/Job title of the receiver**
|
| 56 |
+
- **Company/Organisation where the receiver works**
|
| 57 |
+
- **Areas the receiver is interested in / has expertise in**
|
| 58 |
+
- **Categorize receivers into groups** for consistent instructions.
|
| 59 |
+
"""
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Main Section
|
| 63 |
+
st.markdown(
|
| 64 |
+
"""
|
| 65 |
+
### Steps to Use
|
| 66 |
+
1. **Download the template** from the sidebar.
|
| 67 |
+
2. **Fill out the details** as per the instructions.
|
| 68 |
+
3. **Upload the completed CSV file** below.
|
| 69 |
+
4. Enter a **prompt** to generate personalized invitations.
|
| 70 |
+
"""
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# File uploader for CSV
|
| 74 |
+
uploaded_file = st.file_uploader("π Upload CSV File", type=["csv"])
|
| 75 |
+
user_prompt = st.text_area(
|
| 76 |
+
"βοΈ Enter the prompt for generating invitation texts:",
|
| 77 |
+
"Write professional invitation text tailored for the group.",
|
| 78 |
+
height=150,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Processing and displaying results
|
| 82 |
+
if uploaded_file is not None and user_prompt:
|
| 83 |
+
st.write("β³ Processing your file... Please wait.")
|
| 84 |
+
processed_df = process_csv(uploaded_file, user_prompt)
|
| 85 |
+
|
| 86 |
+
# Display results
|
| 87 |
+
st.write("### Generated Invitations")
|
| 88 |
+
st.dataframe(processed_df, use_container_width=True)
|
| 89 |
+
|
| 90 |
+
# Option to download the processed CSV
|
| 91 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:
|
| 92 |
+
processed_df.to_csv(temp_file.name, index=False)
|
| 93 |
+
temp_file.close() # Ensure the file is properly closed before proceeding
|
| 94 |
+
|
| 95 |
+
st.download_button(
|
| 96 |
+
label="π₯ Download Results CSV",
|
| 97 |
+
data=open(temp_file.name, "rb"),
|
| 98 |
+
file_name="generated_invitations.csv",
|
| 99 |
+
mime="text/csv",
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Safely delete the temporary file
|
| 103 |
+
os.unlink(temp_file.name)
|
| 104 |
+
|
| 105 |
+
# Footer
|
| 106 |
+
st.markdown("---")
|
| 107 |
+
st.markdown(
|
| 108 |
+
"π‘ **Tip:** Ensure your data aligns with the provided template for accurate results."
|
| 109 |
+
)
|