Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pdfplumber
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Function to call the Groq API
|
| 8 |
+
def call_groq_api(data, user_prompt):
|
| 9 |
+
api_url = "https://api.groq.example/modify" # Replace with your Groq API URL
|
| 10 |
+
headers = {
|
| 11 |
+
"Authorization": "Bearer YOUR_API_TOKEN", # Replace with your API token
|
| 12 |
+
"Content-Type": "application/json"
|
| 13 |
+
}
|
| 14 |
+
payload = {
|
| 15 |
+
"data": data,
|
| 16 |
+
"prompt": user_prompt
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
response = requests.post(api_url, json=payload, headers=headers)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
return response.json().get("updated_data", "No updated data returned.")
|
| 23 |
+
else:
|
| 24 |
+
return f"Error from Groq API: {response.status_code} - {response.text}"
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error connecting to Groq API: {str(e)}"
|
| 27 |
+
|
| 28 |
+
# Function to process uploaded files
|
| 29 |
+
def process_file(file, user_prompt):
|
| 30 |
+
if not file:
|
| 31 |
+
return "No file uploaded.", None
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Check file type and extract data
|
| 35 |
+
if file.name.endswith(".xlsx") or file.name.endswith(".xls"):
|
| 36 |
+
df = pd.read_excel(file.name)
|
| 37 |
+
data = df.to_string(index=False)
|
| 38 |
+
elif file.name.endswith(".pdf"):
|
| 39 |
+
data = ""
|
| 40 |
+
with pdfplumber.open(file.name) as pdf:
|
| 41 |
+
for page in pdf.pages:
|
| 42 |
+
data += page.extract_text()
|
| 43 |
+
else:
|
| 44 |
+
return "Unsupported file type. Please upload Excel or PDF files.", None
|
| 45 |
+
|
| 46 |
+
# Call Groq API to modify the data
|
| 47 |
+
updated_data = call_groq_api(data, user_prompt)
|
| 48 |
+
|
| 49 |
+
# Generate a PDF with the updated data
|
| 50 |
+
pdf = FPDF()
|
| 51 |
+
pdf.add_page()
|
| 52 |
+
pdf.set_font("Arial", size=12)
|
| 53 |
+
for line in updated_data.split("\n"):
|
| 54 |
+
pdf.cell(200, 10, txt=line, ln=True)
|
| 55 |
+
|
| 56 |
+
# Save the generated PDF
|
| 57 |
+
pdf_file = "/mnt/data/updated_file.pdf"
|
| 58 |
+
pdf.output(pdf_file)
|
| 59 |
+
return "File processed successfully.", pdf_file
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"An error occurred: {str(e)}", None
|
| 62 |
+
|
| 63 |
+
# Define the Gradio app interface
|
| 64 |
+
def gradio_interface():
|
| 65 |
+
with gr.Blocks() as app:
|
| 66 |
+
gr.Markdown("## Upload an Excel or PDF File and Modify with a Prompt")
|
| 67 |
+
with gr.Row():
|
| 68 |
+
file_input = gr.File(label="Upload File", type="file")
|
| 69 |
+
user_prompt = gr.Textbox(label="Enter Prompt to Modify Data")
|
| 70 |
+
output_msg = gr.Textbox(label="Processing Status")
|
| 71 |
+
file_output = gr.File(label="Download Updated PDF")
|
| 72 |
+
process_button = gr.Button("Process File")
|
| 73 |
+
|
| 74 |
+
process_button.click(
|
| 75 |
+
process_file,
|
| 76 |
+
inputs=[file_input, user_prompt],
|
| 77 |
+
outputs=[output_msg, file_output]
|
| 78 |
+
)
|
| 79 |
+
return app
|
| 80 |
+
|
| 81 |
+
app = gradio_interface()
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
app.launch()
|