import gradio as gr import pandas as pd import pdfplumber from fpdf import FPDF import requests # Function to call the Groq API def call_groq_api(data, user_prompt): api_url = "https://your-real-groq-api-endpoint.com/modify" # Replace with your Groq API URL headers = { "Authorization": "Bearer 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", # Replace with your API token "Content-Type": "application/json" } payload = { "data": data, "prompt": user_prompt } try: response = requests.post(api_url, json=payload, headers=headers) if response.status_code == 200: return response.json().get("updated_data", "No updated data returned.") else: return f"Error from Groq API: {response.status_code} - {response.text}" except Exception as e: return f"Error connecting to Groq API: {str(e)}" # Function to process uploaded files def process_file(file_path, user_prompt): if not file_path: return "No file uploaded.", None try: # Check file type and extract data if file_path.endswith(".xlsx") or file_path.endswith(".xls"): df = pd.read_excel(file_path) data = df.to_string(index=False) elif file_path.endswith(".pdf"): data = "" with pdfplumber.open(file_path) as pdf: for page in pdf.pages: data += page.extract_text() else: return "Unsupported file type. Please upload Excel or PDF files.", None # Call Groq API to modify the data updated_data = call_groq_api(data, user_prompt) # Generate a PDF with the updated data pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) for line in updated_data.split("\n"): pdf.cell(200, 10, txt=line, ln=True) # Save the generated PDF pdf_file = "updated_file.pdf" pdf.output(pdf_file) return "File processed successfully.", pdf_file except Exception as e: return f"An error occurred: {str(e)}", None # Define the Gradio app interface def gradio_interface(): with gr.Blocks() as app: gr.Markdown("## Upload an Excel or PDF File and Modify with a Prompt") with gr.Row(): file_input = gr.File(label="Upload File", type="filepath") # Fixed type user_prompt = gr.Textbox(label="Enter Prompt to Modify Data") output_msg = gr.Textbox(label="Processing Status") file_output = gr.File(label="Download Updated PDF") process_button = gr.Button("Process File") process_button.click( process_file, inputs=[file_input, user_prompt], outputs=[output_msg, file_output] ) return app # Launch the app if __name__ == "__main__": app = gradio_interface() app.launch()