File size: 2,483 Bytes
566be6b
 
 
 
 
 
 
 
 
 
25df7c7
566be6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
38
39
40
41
42
43
44
45
46
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
import gradio as gr
import pandas as pd
import openai
import os
import requests
from docx import Document
from tqdm import tqdm
from google.cloud import vision

# Set up Groq API key (Replace "YOUR_GROQ_API_KEY" with actual API key)
GROQ_API_KEY = "GROQ_API_KEY"

def call_groq_api(prompt):
    url = "https://api.groq.com/v1/chat/completions"
    headers = {"Authorization": f"Bearer {GROQ_API_KEY}", "Content-Type": "application/json"}
    data = {
        "model": "gpt-4",
        "messages": [{"role": "system", "content": prompt}]
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json().get("choices", [{}])[0].get("message", {}).get("content", "")

def extract_text_from_word(file_path):
    doc = Document(file_path)
    extracted_text = []
    for para in doc.paragraphs:
        extracted_text.append(para.text)
    return extracted_text

def extract_data_from_excel(file_path):
    df = pd.read_excel(file_path)
    return df.to_dict(orient="records")

def process_files(word_file, excel_file=None):
    word_data = extract_text_from_word(word_file.name)
    excel_data = extract_data_from_excel(excel_file.name) if excel_file else []

    # Merging Word & Excel Data
    processed_posts = []
    for i, text in enumerate(word_data):
        post_data = {
            "Sr. No.": i + 1,
            "Text": text
        }
        if i < len(excel_data):
            post_data.update(excel_data[i])

        # Use Groq API for additional AI processing
        post_data["AI Analysis"] = call_groq_api(f"Analyze this post: {text}")

        processed_posts.append(post_data)

    # Generate Word document output
    output_doc = Document()
    for post in processed_posts:
        output_doc.add_paragraph(f"Sr. No.: {post['Sr. No.']}")
        output_doc.add_paragraph(f"Text: {post['Text']}")
        output_doc.add_paragraph(f"AI Analysis: {post['AI Analysis']}")
        output_doc.add_paragraph("\n----------------------\n")

    output_path = "processed_output.docx"
    output_doc.save(output_path)
    return output_path

with gr.Blocks() as app:
    gr.Markdown("# Social Media Post Analyzer")
    with gr.Row():
        word_file = gr.File(label="Upload Word File")
        excel_file = gr.File(label="Upload Excel File (Optional)")
    output = gr.File(label="Processed Output")

    process_btn = gr.Button("Process Files")
    process_btn.click(process_files, inputs=[word_file, excel_file], outputs=[output])

app.launch()