n0v33n commited on
Commit
ac823bb
·
1 Parent(s): 001552b
Files changed (1) hide show
  1. app.py +11 -40
app.py CHANGED
@@ -82,7 +82,7 @@ def extract_climate_data(api_key, file_path=None, url=None):
82
  def process_climate_document(api_key, file, url_input):
83
  """
84
  The function `process_climate_document` extracts climate data from either a file or URL input and
85
- generates a structured analysis report.
86
 
87
  :param api_key: The `api_key` parameter is typically a unique identifier or access token that allows
88
  you to authenticate and access a specific API or service. It is used in the
@@ -95,49 +95,20 @@ def process_climate_document(api_key, file, url_input):
95
  provide a URL input for extracting climate data. This URL should point to a document or webpage
96
  containing climate-related information that needs to be analyzed. The function will extract data
97
  from this URL if it is provided
98
- :return: The function `process_climate_document` returns a formatted string containing the analysis
99
- results of a climate document. The output includes information about the document type, title,
100
- organization, publication date, structured data (in JSON format), extracted climate data, chart
101
- analysis, and extracted text. If there is an error during the extraction process, it will return an
102
- error message.
103
  """
104
  if file:
105
  result = extract_climate_data(api_key, file_path=file.name)
106
  elif url_input.strip():
107
  result = extract_climate_data(api_key, url=url_input.strip())
108
  else:
109
- return "Please provide either a file or URL"
 
110
  if "error" in result:
111
- return f"Error: {result['error']}"
112
- output = "# Climate Document Analysis Results\n\n"
113
- if result.get("climate_data"):
114
- data = result['climate_data']
115
- if isinstance(data, dict):
116
- output += f"## Document Overview:\n"
117
- output += f"**Type:** {data.get('document_type', 'N/A')}\n"
118
- output += f"**Title:** {data.get('title', 'N/A')}\n"
119
- output += f"**Organization:** {data.get('organization', 'N/A')}\n"
120
- output += f"**Date:** {data.get('publication_date', 'N/A')}\n\n"
121
- output += "## Complete Structured Data:\n"
122
- output += f"```json\n{json.dumps(data, indent=2)}\n```\n\n"
123
- else:
124
- output += "## Extracted Climate Data:\n"
125
- output += f"```\n{str(data)}\n```\n\n"
126
- if result.get("chart_descriptions"):
127
- output += "## Chart Analysis:\n"
128
- charts = result['chart_descriptions']
129
- if isinstance(charts, list):
130
- for i, chart in enumerate(charts, 1):
131
- if isinstance(chart, dict):
132
- output += f"### Chart {i}:\n{json.dumps(chart, indent=2)}\n\n"
133
- else:
134
- output += f"### Chart {i}:\n{str(chart)}\n\n"
135
- else:
136
- output += f"```\n{str(charts)}\n```\n\n"
137
- if result.get("extracted_text"):
138
- output += "## Extracted Text:\n"
139
- output += f"{result['extracted_text']}...\n\n"
140
- return output
141
 
142
  def analyze_image(api_key, image):
143
  """
@@ -196,7 +167,7 @@ def analyze_image(api_key, image):
196
  "temperature_anomaly": 0.0,
197
  "description": "Error parsing model output."
198
  }
199
- return json.dumps(result, indent=2)
200
  except Exception as e:
201
  error_result = {
202
  "image_type": "error",
@@ -207,7 +178,7 @@ def analyze_image(api_key, image):
207
  "temperature_anomaly": 0.0,
208
  "description": f"Error processing image: {str(e)}"
209
  }
210
- return json.dumps(error_result, indent=2)
211
 
212
  with gr.Blocks(title="Climate Data and Image Analyzer") as demo:
213
  gr.Markdown("# Climate Data and Image Analysis Tool\nAnalyze climate documents or images using Mistral OCR and Pixtral models")
@@ -231,7 +202,7 @@ with gr.Blocks(title="Climate Data and Image Analyzer") as demo:
231
  )
232
  process_btn = gr.Button("Analyze Document", variant="primary")
233
  with gr.Column():
234
- doc_output = gr.Markdown(label="Document Analysis Results")
235
  process_btn.click(
236
  fn=process_climate_document,
237
  inputs=[api_key_input, file_input, url_input],
 
82
  def process_climate_document(api_key, file, url_input):
83
  """
84
  The function `process_climate_document` extracts climate data from either a file or URL input and
85
+ returns structured JSON data.
86
 
87
  :param api_key: The `api_key` parameter is typically a unique identifier or access token that allows
88
  you to authenticate and access a specific API or service. It is used in the
 
95
  provide a URL input for extracting climate data. This URL should point to a document or webpage
96
  containing climate-related information that needs to be analyzed. The function will extract data
97
  from this URL if it is provided
98
+ :return: The function `process_climate_document` returns a JSON object containing the analysis
99
+ results of a climate document including climate_data, chart_descriptions, and extracted_text.
 
 
 
100
  """
101
  if file:
102
  result = extract_climate_data(api_key, file_path=file.name)
103
  elif url_input.strip():
104
  result = extract_climate_data(api_key, url=url_input.strip())
105
  else:
106
+ return {"error": "Please provide either a file or URL"}
107
+
108
  if "error" in result:
109
+ return {"error": result['error']}
110
+
111
+ return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  def analyze_image(api_key, image):
114
  """
 
167
  "temperature_anomaly": 0.0,
168
  "description": "Error parsing model output."
169
  }
170
+ return result
171
  except Exception as e:
172
  error_result = {
173
  "image_type": "error",
 
178
  "temperature_anomaly": 0.0,
179
  "description": f"Error processing image: {str(e)}"
180
  }
181
+ return error_result
182
 
183
  with gr.Blocks(title="Climate Data and Image Analyzer") as demo:
184
  gr.Markdown("# Climate Data and Image Analysis Tool\nAnalyze climate documents or images using Mistral OCR and Pixtral models")
 
202
  )
203
  process_btn = gr.Button("Analyze Document", variant="primary")
204
  with gr.Column():
205
+ doc_output = gr.JSON(label="Document Analysis Results")
206
  process_btn.click(
207
  fn=process_climate_document,
208
  inputs=[api_key_input, file_input, url_input],