Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import re
|
| 5 |
+
import logging
|
| 6 |
+
import nltk
|
| 7 |
+
from docx import Document
|
| 8 |
+
from collections import Counter
|
| 9 |
+
import io
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Initialize logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 17 |
+
|
| 18 |
+
# Download required NLTK resources
|
| 19 |
+
nltk.download("punkt")
|
| 20 |
+
|
| 21 |
+
st.title("AI-Powered Coding Sheet Generator")
|
| 22 |
+
st.write("Enter text or upload a DOCX/Excel file for analysis:")
|
| 23 |
+
|
| 24 |
+
# Option to enable separate tab feature
|
| 25 |
+
separate_tab = st.checkbox("Enable Separate Tab for Summary")
|
| 26 |
+
|
| 27 |
+
input_text = st.text_area("Input Text", height=200)
|
| 28 |
+
uploaded_docx = st.file_uploader("Upload a DOCX file", type=["docx"])
|
| 29 |
+
uploaded_excel = st.file_uploader("Upload an Excel file", type=["xlsx"])
|
| 30 |
+
|
| 31 |
+
output_data = {}
|
| 32 |
+
|
| 33 |
+
# Function to extract text from DOCX
|
| 34 |
+
def extract_text_from_docx(docx_file):
|
| 35 |
+
doc = Document(docx_file)
|
| 36 |
+
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
|
| 37 |
+
|
| 38 |
+
# Function to analyze summary data
|
| 39 |
+
def generate_summary(data):
|
| 40 |
+
total_posts = len(data)
|
| 41 |
+
tones = Counter()
|
| 42 |
+
languages = Counter()
|
| 43 |
+
frames = Counter()
|
| 44 |
+
frame_focus = {"Major Focus": Counter(), "Significant Focus": Counter(), "Minor Mention": Counter(), "Not Applicable": Counter()}
|
| 45 |
+
|
| 46 |
+
for post in data.values():
|
| 47 |
+
tones.update(post.get("Tone", []))
|
| 48 |
+
languages[post.get("Language", "Unknown")] += 1
|
| 49 |
+
frame_mapping = post.get("FramesMapping", {})
|
| 50 |
+
for frame, focus in frame_mapping.items():
|
| 51 |
+
frames[frame] += 1
|
| 52 |
+
frame_focus[focus][frame] += 1
|
| 53 |
+
|
| 54 |
+
abstract = f"This document contains {total_posts} posts. The most commonly used tone is '{tones.most_common(1)}'. "
|
| 55 |
+
abstract += f"The most frequently mentioned frame is '{frames.most_common(1)}'. Languages used include {list(languages.keys())}."
|
| 56 |
+
|
| 57 |
+
return total_posts, tones, languages, frames, frame_focus, abstract
|
| 58 |
+
|
| 59 |
+
# Function to create an Excel summary
|
| 60 |
+
def create_summary_excel(summary_data):
|
| 61 |
+
total_posts, tones, languages, frames, frame_focus, abstract = summary_data
|
| 62 |
+
with io.BytesIO() as buffer:
|
| 63 |
+
writer = pd.ExcelWriter(buffer, engine='xlsxwriter')
|
| 64 |
+
|
| 65 |
+
pd.DataFrame(tones.items(), columns=["Tone", "Count"]).to_excel(writer, sheet_name="Tones", index=False)
|
| 66 |
+
pd.DataFrame(languages.items(), columns=["Language", "Count"]).to_excel(writer, sheet_name="Languages", index=False)
|
| 67 |
+
pd.DataFrame(frames.items(), columns=["Frame", "Count"]).to_excel(writer, sheet_name="Frames", index=False)
|
| 68 |
+
|
| 69 |
+
for focus, counts in frame_focus.items():
|
| 70 |
+
pd.DataFrame(counts.items(), columns=["Frame", "Count"]).to_excel(writer, sheet_name=focus, index=False)
|
| 71 |
+
|
| 72 |
+
pd.DataFrame({"Abstract": [abstract]}).to_excel(writer, sheet_name="Abstract", index=False)
|
| 73 |
+
|
| 74 |
+
writer.close()
|
| 75 |
+
buffer.seek(0)
|
| 76 |
+
return buffer.getvalue()
|
| 77 |
+
|
| 78 |
+
if uploaded_docx:
|
| 79 |
+
docx_text = extract_text_from_docx(uploaded_docx)
|
| 80 |
+
summary_data = generate_summary({"Uploaded DOCX": {"Full Caption": docx_text}})
|
| 81 |
+
if separate_tab:
|
| 82 |
+
with st.expander("Summary Tab"):
|
| 83 |
+
st.write(f"Total Posts: {summary_data[0]}")
|
| 84 |
+
st.write(f"Tones: {dict(summary_data[1])}")
|
| 85 |
+
st.write(f"Languages: {dict(summary_data[2])}")
|
| 86 |
+
st.write(f"Frames: {dict(summary_data[3])}")
|
| 87 |
+
st.write(f"Abstract: {summary_data[5]}")
|
| 88 |
+
excel_data = create_summary_excel(summary_data)
|
| 89 |
+
st.download_button("Download Summary as Excel", data=excel_data, file_name="summary.xlsx", mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|