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://your-real-groq-api-endpoint.com/modify" # Replace with
|
10 |
headers = {
|
11 |
-
"Authorization": "Bearer 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", # Replace with
|
12 |
"Content-Type": "application/json"
|
13 |
}
|
14 |
payload = {
|
@@ -17,18 +17,13 @@ def call_groq_api(data, user_prompt):
|
|
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
|
31 |
-
return f"
|
32 |
|
33 |
# Function to process uploaded files
|
34 |
def process_file(file_path, user_prompt):
|
@@ -51,15 +46,12 @@ 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.
|
63 |
|
64 |
# Save the generated PDF
|
65 |
pdf_file = "updated_file.pdf"
|
@@ -73,7 +65,7 @@ def gradio_interface():
|
|
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")
|
@@ -89,4 +81,4 @@ def gradio_interface():
|
|
89 |
# Launch the app
|
90 |
if __name__ == "__main__":
|
91 |
app = gradio_interface()
|
92 |
-
app.launch()
|
|
|
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 your Groq API URL
|
10 |
headers = {
|
11 |
+
"Authorization": "Bearer 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", # Replace with your API token
|
12 |
"Content-Type": "application/json"
|
13 |
}
|
14 |
payload = {
|
|
|
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_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.cell(200, 10, txt=line, ln=True)
|
55 |
|
56 |
# Save the generated PDF
|
57 |
pdf_file = "updated_file.pdf"
|
|
|
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") # Fixed type
|
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")
|
|
|
81 |
# Launch the app
|
82 |
if __name__ == "__main__":
|
83 |
app = gradio_interface()
|
84 |
+
app.launch()
|