File size: 2,937 Bytes
b003454
 
 
 
 
 
 
 
51fdd7c
b003454
51fdd7c
b003454
 
 
 
 
 
 
 
 
 
 
 
 
51fdd7c
 
b003454
 
3b8a435
 
b003454
 
 
 
3b8a435
 
b003454
3b8a435
b003454
3b8a435
b003454
 
 
 
 
 
 
 
 
 
 
 
 
51fdd7c
b003454
 
b6c7f56
b003454
 
 
 
 
 
 
 
 
 
51fdd7c
b003454
 
 
 
 
 
 
 
 
 
 
 
3b8a435
b003454
3b8a435
51fdd7c
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
76
77
78
79
80
81
82
83
84
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()