Spaces:
Sleeping
Sleeping
| 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() |