Spaces:
GIZ
/
Running on CPU Upgrade

Romulan12 commited on
Commit
a4e8dda
·
1 Parent(s): 1aa747d

added formattor

Browse files
Files changed (1) hide show
  1. app.py +21 -48
app.py CHANGED
@@ -24,23 +24,30 @@ SAMPLE_QUESTIONS = {
24
  ]
25
  }
26
 
 
 
 
 
 
 
 
27
  def format_whisp_statistics(df):
28
- """Format WhispAPI statistics into a standardized, readable text"""
29
  try:
30
- # Extract required indicators from API response
31
- country = df['Country'].iloc[0]
32
- admin_level = df['Admin_Level_1'].iloc[0]
33
- area = round(float(df['Area'].iloc[0]), 2) if 'Area' in df.columns else "Not available"
34
-
35
- risk_level = df['risk_level'].iloc[0] if 'risk_level' in df.columns else "Not available"
36
- risk_pcrop = df['risk_pcrop'].iloc[0] if 'risk_pcrop' in df.columns else "Not available"
37
- risk_acrop = df['risk_acrop'].iloc[0] if 'risk_acrop' in df.columns else "Not available"
38
- risk_timber = df['risk_timber'].iloc[0] if 'risk_timber' in df.columns else "Not available"
39
 
40
- # TMF_def_after_2020 is a risk category, not hectares
41
- def_after_2020 = df['TMF_def_after_2020'].iloc[0] if 'TMF_def_after_2020' in df.columns else "Not available"
 
 
 
42
 
43
- # Format the text output
44
  output = f"""📊 Analysis Results for Your Plot
45
 
46
  Plot Information:
@@ -61,40 +68,6 @@ Deforestation Analysis:
61
  except Exception as e:
62
  return f"Error formatting statistics: {str(e)}"
63
 
64
- # def format_whisp_statistics(df):
65
- # """Format WhispAPI statistics into a standardized, readable text"""
66
- # try:
67
- # # Extract required indicators from API response
68
- # country = df['Country'].iloc[0]
69
- # admin_level = df['Admin_Level_1'].iloc[0]
70
- # area = round(df['Area'].iloc[0], 2)
71
- # risk_level = df['risk_level'].iloc[0] if 'risk_level' in df.columns else "Not available"
72
- # risk_pcrop = df['risk_pcrop'].iloc[0] if 'risk_pcrop' in df.columns else "Not available"
73
- # risk_acrop = df['risk_acrop'].iloc[0] if 'risk_acrop' in df.columns else "Not available"
74
- # risk_timber = df['risk_timber'].iloc[0] if 'risk_timber' in df.columns else "Not available"
75
- # deforestation_after_2020 = df['TMF_def_after_2020'].iloc[0] if 'TMF_def_after_2020' in df.columns else 0
76
-
77
- # # Format the text output
78
- # output = f"""📊 Analysis Results for Your Plot
79
-
80
- # Plot Information:
81
- # - Country: {country}
82
- # - Administrative Region: {admin_level}
83
- # - Total Area: {area} hectares
84
-
85
- # Risk Assessment:
86
- # - Overall Risk Level: {risk_level}
87
- # - Permanent Crop Risk: {risk_pcrop}
88
- # - Annual Crop Risk: {risk_acrop}
89
- # - Timber Risk: {risk_timber}
90
-
91
- # Deforestation Analysis:
92
- # - Deforestation after 2020: {round(deforestation_after_2020, 2)} hectares
93
- # """
94
- # return output
95
- # except Exception as e:
96
- # return f"Error formatting statistics: {str(e)}"
97
-
98
  def handle_geojson_upload(file):
99
  """Handle GeoJSON file upload and call WHISP API"""
100
  if file is not None:
@@ -139,7 +112,7 @@ def retrieve_paragraphs(query):
139
 
140
  try:
141
  # Call the API with the uploaded file
142
- client = Client("https://giz-eudr-retriever.hf.space/")
143
  result = client.predict(
144
  query=query,
145
  reports_filter="",
 
24
  ]
25
  }
26
 
27
+ def get_value(df, colname):
28
+ """Fetch value from WhispAPI-style Column/Value dataframe"""
29
+ if "Column" in df.columns and "Value" in df.columns:
30
+ match = df.loc[df["Column"] == colname, "Value"]
31
+ if not match.empty:
32
+ return match.values[0]
33
+ return "Not available"
34
  def format_whisp_statistics(df):
35
+ """Format WhispAPI statistics into readable text for end-users"""
36
  try:
37
+ country = get_value(df, "Country")
38
+ admin_level = get_value(df, "Admin_Level_1")
39
+ area = get_value(df, "Area")
40
+ try:
41
+ area = round(float(area), 2)
42
+ except:
43
+ pass # keep as string if not convertible
 
 
44
 
45
+ risk_level = get_value(df, "risk_level")
46
+ risk_pcrop = get_value(df, "risk_pcrop")
47
+ risk_acrop = get_value(df, "risk_acrop")
48
+ risk_timber = get_value(df, "risk_timber")
49
+ def_after_2020 = get_value(df, "TMF_def_after_2020")
50
 
 
51
  output = f"""📊 Analysis Results for Your Plot
52
 
53
  Plot Information:
 
68
  except Exception as e:
69
  return f"Error formatting statistics: {str(e)}"
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  def handle_geojson_upload(file):
72
  """Handle GeoJSON file upload and call WHISP API"""
73
  if file is not None:
 
112
 
113
  try:
114
  # Call the API with the uploaded file
115
+ client = Client("https://giz-chatfed-retriever.hf.space/")
116
  result = client.predict(
117
  query=query,
118
  reports_filter="",