Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
from docx import Document
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
from google.cloud import vision
|
| 9 |
+
|
| 10 |
+
# Set up Groq API key (Replace "YOUR_GROQ_API_KEY" with actual API key)
|
| 11 |
+
GROQ_API_KEY = "YOUR_GROQ_API_KEY"
|
| 12 |
+
|
| 13 |
+
def call_groq_api(prompt):
|
| 14 |
+
url = "https://api.groq.com/v1/chat/completions"
|
| 15 |
+
headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
|
| 16 |
+
data = {
|
| 17 |
+
"model": "gpt-4",
|
| 18 |
+
"messages": [{"role": "system", "content": prompt}]
|
| 19 |
+
}
|
| 20 |
+
response = requests.post(url, headers=headers, json=data)
|
| 21 |
+
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 22 |
+
|
| 23 |
+
def extract_text_from_word(file_path):
|
| 24 |
+
doc = Document(file_path)
|
| 25 |
+
extracted_text = []
|
| 26 |
+
for para in doc.paragraphs:
|
| 27 |
+
extracted_text.append(para.text)
|
| 28 |
+
return extracted_text
|
| 29 |
+
|
| 30 |
+
def extract_data_from_excel(file_path):
|
| 31 |
+
df = pd.read_excel(file_path)
|
| 32 |
+
return df.to_dict(orient="records")
|
| 33 |
+
|
| 34 |
+
def process_files(word_file, excel_file=None):
|
| 35 |
+
word_data = extract_text_from_word(word_file.name)
|
| 36 |
+
excel_data = extract_data_from_excel(excel_file.name) if excel_file else []
|
| 37 |
+
|
| 38 |
+
# Merging Word & Excel Data
|
| 39 |
+
processed_posts = []
|
| 40 |
+
for i, text in enumerate(word_data):
|
| 41 |
+
post_data = {
|
| 42 |
+
"Sr. No.": i + 1,
|
| 43 |
+
"Text": text
|
| 44 |
+
}
|
| 45 |
+
if i < len(excel_data):
|
| 46 |
+
post_data.update(excel_data[i])
|
| 47 |
+
|
| 48 |
+
# Use Groq API for additional AI processing
|
| 49 |
+
post_data["AI Analysis"] = call_groq_api(f"Analyze this post: {text}")
|
| 50 |
+
|
| 51 |
+
processed_posts.append(post_data)
|
| 52 |
+
|
| 53 |
+
# Generate Word document output
|
| 54 |
+
output_doc = Document()
|
| 55 |
+
for post in processed_posts:
|
| 56 |
+
output_doc.add_paragraph(f"Sr. No.: {post['Sr. No.']}")
|
| 57 |
+
output_doc.add_paragraph(f"Text: {post['Text']}")
|
| 58 |
+
output_doc.add_paragraph(f"AI Analysis: {post['AI Analysis']}")
|
| 59 |
+
output_doc.add_paragraph("\n----------------------\n")
|
| 60 |
+
|
| 61 |
+
output_path = "processed_output.docx"
|
| 62 |
+
output_doc.save(output_path)
|
| 63 |
+
return output_path
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown("# Social Media Post Analyzer")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
word_file = gr.File(label="Upload Word File")
|
| 69 |
+
excel_file = gr.File(label="Upload Excel File (Optional)")
|
| 70 |
+
output = gr.File(label="Processed Output")
|
| 71 |
+
|
| 72 |
+
process_btn = gr.Button("Process Files")
|
| 73 |
+
process_btn.click(process_files, inputs=[word_file, excel_file], outputs=[output])
|
| 74 |
+
|
| 75 |
+
app.launch()
|