Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,9 @@ import requests
|
|
| 6 |
|
| 7 |
# Function to call the Groq API
|
| 8 |
def call_groq_api(data, user_prompt):
|
| 9 |
-
api_url = "https://api.
|
| 10 |
headers = {
|
| 11 |
-
"Authorization": "Bearer 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", # Replace with
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
payload = {
|
|
@@ -17,13 +17,18 @@ def call_groq_api(data, 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
|
| 26 |
-
return f"
|
| 27 |
|
| 28 |
# Function to process uploaded files
|
| 29 |
def process_file(file_path, user_prompt):
|
|
@@ -46,12 +51,15 @@ def process_file(file_path, user_prompt):
|
|
| 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.
|
| 55 |
|
| 56 |
# Save the generated PDF
|
| 57 |
pdf_file = "updated_file.pdf"
|
|
@@ -65,7 +73,7 @@ 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="filepath")
|
| 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")
|
|
|
|
| 6 |
|
| 7 |
# Function to call the Groq API
|
| 8 |
def call_groq_api(data, user_prompt):
|
| 9 |
+
api_url = "https://your-real-groq-api-endpoint.com/modify" # Replace with actual API URL
|
| 10 |
headers = {
|
| 11 |
+
"Authorization": "Bearer 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", # Replace with actual API token
|
| 12 |
"Content-Type": "application/json"
|
| 13 |
}
|
| 14 |
payload = {
|
|
|
|
| 17 |
}
|
| 18 |
|
| 19 |
try:
|
| 20 |
+
# Debugging: Log payload and API call
|
| 21 |
+
print("Calling Groq API with payload:", payload)
|
| 22 |
+
|
| 23 |
response = requests.post(api_url, json=payload, headers=headers)
|
| 24 |
+
|
| 25 |
if response.status_code == 200:
|
| 26 |
+
# Extract updated data
|
| 27 |
return response.json().get("updated_data", "No updated data returned.")
|
| 28 |
else:
|
| 29 |
return f"Error from Groq API: {response.status_code} - {response.text}"
|
| 30 |
+
except requests.exceptions.RequestException as e:
|
| 31 |
+
return f"Request exception: {str(e)}"
|
| 32 |
|
| 33 |
# Function to process uploaded files
|
| 34 |
def process_file(file_path, user_prompt):
|
|
|
|
| 51 |
# Call Groq API to modify the data
|
| 52 |
updated_data = call_groq_api(data, user_prompt)
|
| 53 |
|
| 54 |
+
if "Error" in updated_data:
|
| 55 |
+
return updated_data, None # Return API error
|
| 56 |
+
|
| 57 |
# Generate a PDF with the updated data
|
| 58 |
pdf = FPDF()
|
| 59 |
pdf.add_page()
|
| 60 |
pdf.set_font("Arial", size=12)
|
| 61 |
for line in updated_data.split("\n"):
|
| 62 |
+
pdf.multi_cell(0, 10, txt=line) # Use multi_cell for proper text wrapping
|
| 63 |
|
| 64 |
# Save the generated PDF
|
| 65 |
pdf_file = "updated_file.pdf"
|
|
|
|
| 73 |
with gr.Blocks() as app:
|
| 74 |
gr.Markdown("## Upload an Excel or PDF File and Modify with a Prompt")
|
| 75 |
with gr.Row():
|
| 76 |
+
file_input = gr.File(label="Upload File", type="filepath")
|
| 77 |
user_prompt = gr.Textbox(label="Enter Prompt to Modify Data")
|
| 78 |
output_msg = gr.Textbox(label="Processing Status")
|
| 79 |
file_output = gr.File(label="Download Updated PDF")
|