Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 25,775 Bytes
c83f30f 054da8d 4cb1652 a1c7621 054da8d 4cb1652 5a4f54c c71c0cf 3eaba6e 1aa747d a4e8dda 36e8f83 a1c7621 a4e8dda a1c7621 25982d3 a4e8dda 25982d3 a4e8dda 25982d3 a4e8dda 25982d3 0b1d4d1 a4e8dda 25982d3 0b1d4d1 25982d3 bebc7ce 25982d3 bebc7ce a1c7621 25982d3 a1c7621 25982d3 bebc7ce a1c7621 25982d3 bebc7ce 25982d3 bebc7ce 25982d3 bebc7ce 25982d3 bebc7ce 25982d3 a1c7621 25982d3 a1c7621 054da8d 4cb1652 054da8d 4cb1652 c71c0cf 4cb1652 c71c0cf 4cb1652 c71c0cf a1c7621 4cb1652 a1c7621 4cb1652 c71c0cf 4cb1652 c71c0cf 054da8d c71c0cf a4b0a09 c71c0cf 1b3f70d 0b1d4d1 bebc7ce 0b1d4d1 a4b0a09 0b1d4d1 c71c0cf 0b1d4d1 bebc7ce 0b1d4d1 50572ac a4b0a09 0b1d4d1 bebc7ce 0b1d4d1 bebc7ce 054da8d 3eaba6e c71c0cf 3eaba6e 054da8d 3eaba6e c71c0cf 3eaba6e 054da8d 1b3f70d c71c0cf 1b3f70d 054da8d 3eaba6e 054da8d c71c0cf 054da8d 3eaba6e 054da8d c71c0cf 1b3f70d a1c7621 bebc7ce a1c7621 0b1d4d1 1b3f70d 0b1d4d1 1b3f70d 0b1d4d1 1b3f70d a1c7621 1b3f70d 3eaba6e c71c0cf 3eaba6e bebc7ce 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e 054da8d c71c0cf 054da8d 3eaba6e 054da8d 3eaba6e 054da8d c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 054da8d 3eaba6e c71c0cf 054da8d 3eaba6e 054da8d c71c0cf 3eaba6e 054da8d c71c0cf 3eaba6e 054da8d c71c0cf 054da8d c71c0cf 054da8d c71c0cf 3eaba6e c71c0cf 054da8d c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e 054da8d 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf bebc7ce 4cb1652 bebc7ce 054da8d c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e c71c0cf 3eaba6e 054da8d c71c0cf 3eaba6e bebc7ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
import gradio as gr
import time
import pandas as pd
import asyncio
from uuid import uuid4
from gradio_client import Client, handle_file
# Sample questions for examples
SAMPLE_QUESTIONS = {
"Deforestation Analysis": [
"What are the main deforestation hotspots in Ecuador?",
"Show me deforestation trends in the uploaded area",
"What commodities are driving deforestation in Guatemala?"
],
"EUDR Compliance": [
"What are the key EUDR requirements for coffee imports?",
"How do I prove due diligence for my supply chain?",
"What documentation is needed for EUDR compliance?"
],
"Risk Assessment": [
"What is the deforestation risk level in this region?",
"How do I assess supply chain risks?",
"What are the compliance deadlines?"
]
}
def get_value(df, colname):
"""Fetch value from WhispAPI-style Column/Value dataframe"""
if "Column" in df.columns and "Value" in df.columns:
match = df.loc[df["Column"] == colname, "Value"]
if not match.empty:
return match.values[0]
return "Not available"
def format_whisp_statistics(df):
"""Format WhispAPI statistics into readable text for end-users"""
try:
# Country code mapping for better display
country_codes = {
'HND': 'Honduras',
'GTM': 'Guatemala',
'ECU': 'Ecuador',
'COL': 'Colombia',
'PER': 'Peru',
'BRA': 'Brazil',
'BOL': 'Bolivia',
'CRI': 'Costa Rica',
'PAN': 'Panama',
'NIC': 'Nicaragua'
}
country_raw = get_value(df, "Country")
country = country_codes.get(country_raw, country_raw)
admin_level = get_value(df, "Admin_Level_1")
area_raw = get_value(df, "Area")
# Format area with proper rounding and units
try:
area_num = float(area_raw)
if area_num < 1:
area_text = f"{area_num:.3f} hectares"
elif area_num < 100:
area_text = f"{area_num:.2f} hectares"
else:
area_text = f"{area_num:,.1f} hectares"
except:
area_text = str(area_raw) if area_raw != "Not available" else "Not available"
risk_level = get_value(df, "risk_level")
risk_pcrop = get_value(df, "risk_pcrop")
risk_acrop = get_value(df, "risk_acrop")
risk_timber = get_value(df, "risk_timber")
def_after_2020_raw = get_value(df, "TMF_def_after_2020")
# Helper function to format risk levels with colors/emojis
def format_risk(risk_val):
if not risk_val or risk_val in ["Not available", "not available"]:
return "π **Not Available** *(Analysis pending)*"
elif isinstance(risk_val, str):
risk_lower = risk_val.lower().strip()
if risk_lower == "low":
return "π’ **Low Risk**"
elif risk_lower == "medium":
return "π‘ **Medium Risk**"
elif risk_lower == "high":
return "π **High Risk**"
elif risk_lower == "very high":
return "π΄ **Very High Risk**"
elif risk_lower == "more_info_needed":
return "π **Assessment Pending** *(More data needed)*"
else:
return f"βΉοΈ **{risk_val.title()}**"
return str(risk_val)
# Format deforestation data
def format_deforestation(def_val):
if not def_val or def_val in ["Not available", "not available"]:
return "π **No Data Available**"
try:
def_num = float(def_val)
if def_num == 0:
return "β
**No Recent Deforestation Detected**"
elif def_num < 0.1:
return f"β οΈ **{def_num:.3f} hectares detected**"
else:
return f"β οΈ **{def_num:.2f} hectares detected**"
except:
return f"βΉοΈ **{def_val}**"
# Create EUDR compliance assessment
def get_compliance_status(def_after_2020):
try:
def_num = float(def_after_2020)
if def_num == 0:
return "β
**COMPLIANT** - No recent deforestation detected"
elif def_num > 0:
return "β οΈ **REQUIRES ATTENTION** - Recent deforestation detected"
except:
return "π **ASSESSMENT NEEDED** - Insufficient data for compliance determination"
deforestation_formatted = format_deforestation(def_after_2020_raw)
compliance_status = get_compliance_status(def_after_2020_raw)
output = f"""π **Geographic Analysis Results**
## π **Location Details**
- **Country**: {country}
- **Administrative Region**: {admin_level}
- **Total Area**: {area_text}
## β οΈ **Deforestation Risk Assessment**
*Risk levels are based on historical patterns, environmental factors, and land use data*
- **Overall Risk**: {format_risk(risk_level)}
- **Permanent Crops**: {format_risk(risk_pcrop)}
*Coffee, cocoa, palm oil, fruit trees*
- **Annual Crops**: {format_risk(risk_acrop)}
*Soy, corn, rice, vegetables*
- **Timber Extraction**: {format_risk(risk_timber)}
*Logging and wood harvesting*
## π³ **EUDR Compliance Analysis**
*Based on Tropical Moist Forest satellite monitoring*
**Recent Deforestation (2020-Present):** {deforestation_formatted}
**EUDR Compliance Status:** {compliance_status}
---
### π‘ **Key Insights**
- **For Suppliers**: {compliance_status.split(' - ')[1] if ' - ' in compliance_status else 'Review compliance requirements carefully'}
- **Risk Factors**: Focus on {', '.join([t.split('*')[1].strip('*') for t in [risk_pcrop, risk_acrop, risk_timber] if 'High' in format_risk(t)])} if any high-risk activities detected
- **Next Steps**: {"Conduct additional due diligence if recent deforestation is detected" if "ATTENTION" in compliance_status else "Continue monitoring and maintain documentation"}
"""
return output
except Exception as e:
return f"β **Analysis Error**\n\nUnable to process the geographic data: {str(e)}\n\nπ **Troubleshooting:**\n- Verify your GeoJSON file format\n- Check file size (should be < 10MB)\n- Ensure coordinates are valid\n\nPlease try uploading again or contact support."
def handle_geojson_upload(file):
"""Handle GeoJSON file upload and call WHISP API"""
if file is not None:
try:
# Initialize WHISP API client
client = Client("https://giz-chatfed-whisp.hf.space/")
# Call the API with the uploaded file
result = client.predict(
file=handle_file(file.name),
api_name="/get_statistics"
)
# Convert result to DataFrame
df = pd.DataFrame(result['data'], columns=result['headers'])
# Format statistics into readable text
formatted_stats = format_whisp_statistics(df)
return (
formatted_stats, # Keep formatted statistics for chat
gr.update(visible=True), # Keep status visible
gr.update(visible=False) # Always hide results table
)
except Exception as e:
error_msg = f"β Error processing GeoJSON file: {str(e)}"
return (
error_msg,
gr.update(visible=True), # upload_status
gr.update(visible=False) # results_table
)
else:
return (
"",
gr.update(visible=False), # upload_status
gr.update(visible=False) # results_table
)
def retrieve_paragraphs(query):
"""Connect to retriever and retrieve paragraphs"""
try:
# Call the API with the uploaded file
client = Client("https://giz-chatfed-retriever.hf.space/")
result = client.predict(
query=query,
reports_filter="",
sources_filter="",
subtype_filter="",
year_filter="",
api_name="/retrieve"
)
print(result)
return (
result,
gr.update(visible=True), # Keep status visible
gr.update(visible=False) # Always hide results table
)
except Exception as e:
error_msg = f"Error retrieving paragraphs: {str(e)}"
return (
error_msg,
gr.update(visible=True), # upload_status
gr.update(visible=False) # results_table
)
def start_chat(query, history):
"""Start a new chat interaction"""
history = history + [(query, None)]
return gr.update(interactive=False), gr.update(selected=1), history
def finish_chat():
"""Finish chat and reset input"""
return gr.update(interactive=True, value="")
async def chat_response(query, history, method, country, uploaded_file):
"""Generate chat response based on method and inputs"""
# Validate inputs
if method == "Upload GeoJSON":
if uploaded_file is None:
warning_message = "β οΈ **No GeoJSON file uploaded.** Please upload a GeoJSON file first."
history[-1] = (query, warning_message)
yield history, ""
return
else: # "Talk to Reports"
if not country:
warning_message = "β οΈ **No country selected.** Please select a country to analyze reports."
history[-1] = (query, warning_message)
yield history, ""
return
# Handle GeoJSON upload β put formatted stats into chat
if method == "Upload GeoJSON" and uploaded_file:
try:
# handle_geojson_upload returns a tuple (formatted_stats, gr.update(), gr.update())
stats_result = handle_geojson_upload(uploaded_file)
formatted_stats = stats_result[0] # Extract just the formatted statistics
response = formatted_stats
except Exception as e:
response = f"Error processing file: {str(e)}"
# Handle "Talk to Reports"
else:
try:
response = f"Based on EUDR reports for {country}, I can help you understand deforestation patterns and compliance requirements. Your question: '{query}' is being analyzed..."
retrieved_info = retrieve_paragraphs(query)[0]
response = retrieved_info
except Exception as e:
response = f"Error retrieving information: {str(e)}"
# Stream response word by word into the chat
words = response.split()
for i in range(len(words)):
history[-1] = (query, " ".join(words[:i+1]))
yield history, "**Sources:** Sample source documents would appear here..."
await asyncio.sleep(0.05)
def auto_analyze_file(file, history):
"""Automatically analyze uploaded GeoJSON file and add results to chat"""
if file is not None:
# Add a system message indicating file upload
analysis_query = "π GeoJSON file uploaded - analyzing..."
history = history + [(analysis_query, None)]
return history, analysis_query
return history, ""
def auto_display_results(query, history, uploaded_file):
"""Display analysis results automatically after file upload"""
if uploaded_file is not None and query.startswith("π GeoJSON file uploaded"):
try:
# Get the analysis results
stats_result = handle_geojson_upload(uploaded_file)
formatted_stats = stats_result[0] # Extract formatted statistics
# Update the chat with results
history[-1] = (query, formatted_stats)
return history, "**Sources:** WhispAPI Analysis Results"
except Exception as e:
error_msg = f"β Error processing GeoJSON file: {str(e)}"
history[-1] = (query, error_msg)
return history, ""
return history, ""
def toggle_search_method(method):
"""Toggle between GeoJSON upload and country selection"""
if method == "Upload GeoJSON":
return (
gr.update(visible=True), # geojson_section
gr.update(visible=False), # reports_section
gr.update(value=None), # dropdown_country
)
else: # "Talk to Reports"
return (
gr.update(visible=False), # geojson_section
gr.update(visible=True), # reports_section
gr.update(), # dropdown_country
)
def change_sample_questions(key):
"""Update visible examples based on selected category"""
keys = list(SAMPLE_QUESTIONS.keys())
index = keys.index(key)
visible_bools = [False] * len(keys)
visible_bools[index] = True
return [gr.update(visible=visible_bools[i]) for i in range(len(keys))]
# Set up Gradio Theme
theme = gr.themes.Base(
primary_hue="green",
secondary_hue="blue",
font=[gr.themes.GoogleFont("Poppins"), "ui-sans-serif", "system-ui", "sans-serif"],
text_size=gr.themes.utils.sizes.text_sm,
)
# Custom CSS for DataFrame styling
custom_css = """
/* DataFrame text sizing - Modify these values to change text size */
.dataframe table {
font-size: 12px !important; /* Change this value (e.g., 10px, 14px, 16px) */
}
.dataframe th {
font-size: 13px !important; /* Header text size */
font-weight: 600 !important;
}
.dataframe td {
font-size: 12px !important; /* Cell text size */
padding: 8px !important; /* Cell padding */
}
/* Alternative size classes - change elem_classes="dataframe-small" in DataFrame component */
.dataframe-small table { font-size: 10px !important; }
.dataframe-small th { font-size: 11px !important; }
.dataframe-small td { font-size: 10px !important; }
.dataframe-medium table { font-size: 14px !important; }
.dataframe-medium th { font-size: 15px !important; }
.dataframe-medium td { font-size: 14px !important; }
.dataframe-large table { font-size: 16px !important; }
.dataframe-large th { font-size: 17px !important; }
.dataframe-large td { font-size: 16px !important; }
"""
init_prompt = """
Hello, I am EUDR Q&A, an AI-powered conversational assistant designed to help you understand EU Deforestation Regulation compliance and analysis. I will answer your questions by using **EUDR reports and uploaded GeoJSON files**.
π‘ **How to use (tabs on right)**
- **Data Sources**: Choose to either upload a GeoJSON file for analysis or talk to EUDR reports filtered by country.
- **Examples**: Select from curated example questions across different categories.
- **Sources**: View the content sources used to generate answers for fact-checking.
β οΈ For limitations and data collection information, please check the **Disclaimer** tab.
"""
with gr.Blocks(title="EUDR Q&A", theme=theme, css=custom_css) as demo:
# Main Chat Interface
with gr.Tab("EUDR Q&A"):
with gr.Row():
# Left column - Chat interface (2/3 width)
with gr.Column(scale=2):
chatbot = gr.Chatbot(
value=[(None, init_prompt)],
show_copy_button=True,
show_label=False,
layout="panel",
avatar_images=(None, "π³"),
height=500
)
# Feedback UI
with gr.Column():
with gr.Row(visible=False) as feedback_row:
gr.Markdown("Was this response helpful?")
with gr.Row():
okay_btn = gr.Button("π Okay", size="sm")
not_okay_btn = gr.Button("π Not to expectations", size="sm")
feedback_thanks = gr.Markdown("Thanks for the feedback!", visible=False)
# Input textbox
with gr.Row():
textbox = gr.Textbox(
placeholder="Ask me anything about EUDR compliance or upload your GeoJSON for analysis!",
show_label=False,
scale=7,
lines=1,
interactive=True
)
# Right column - Controls and tabs (1/3 width)
with gr.Column(scale=1, variant="panel"):
with gr.Tabs() as tabs:
# Data Sources Tab
with gr.Tab("Data Sources", id=2):
search_method = gr.Radio(
choices=["Upload GeoJSON", "Talk to Reports"],
label="Choose data source",
info="Upload a GeoJSON file for analysis or select country-specific EUDR reports",
value="Upload GeoJSON",
)
# GeoJSON Upload Section
with gr.Group(visible=True) as geojson_section:
uploaded_file = gr.File(
label="Upload GeoJSON File",
file_types=[".geojson", ".json"],
file_count="single"
)
upload_status = gr.Markdown("", visible=False)
# Results table for WHISP API response
results_table = gr.DataFrame(
label="Analysis Results",
visible=False,
interactive=False,
wrap=True,
elem_classes="dataframe"
)
# Talk to Reports Section
with gr.Group(visible=False) as reports_section:
dropdown_country = gr.Dropdown(
["Ecuador", "Guatemala"],
label="Select Country",
value=None,
interactive=True,
)
# Examples Tab
with gr.Tab("Examples", id=0):
examples_hidden = gr.Textbox(visible=False)
first_key = list(SAMPLE_QUESTIONS.keys())[0]
dropdown_samples = gr.Dropdown(
SAMPLE_QUESTIONS.keys(),
value=first_key,
interactive=True,
show_label=True,
label="Select a category of sample questions"
)
# Create example sections
sample_groups = []
for i, (key, questions) in enumerate(SAMPLE_QUESTIONS.items()):
examples_visible = True if i == 0 else False
with gr.Row(visible=examples_visible) as group_examples:
gr.Examples(
questions,
[examples_hidden],
examples_per_page=8,
run_on_click=False,
)
sample_groups.append(group_examples)
# Sources Tab
with gr.Tab("Sources", id=1):
sources_textbox = gr.HTML(
show_label=False,
value="Source documents will appear here after you ask a question..."
)
# Guidelines Tab
with gr.Tab("Guidelines"):
gr.Markdown("""
#### Welcome to EUDR Q&A!
This AI-powered assistant helps you understand EU Deforestation Regulation compliance and analyze geographic data.
## π¬ How to Ask Effective Questions
| β Less Effective | β
More Effective |
|------------------|-------------------|
| "What is deforestation?" | "What are the main deforestation hotspots in Ecuador?" |
| "Tell me about compliance" | "What EUDR requirements apply to coffee imports from Guatemala?" |
| "Show me data" | "What is the deforestation rate in the uploaded region?" |
## π Using Data Sources
**Upload GeoJSON:** Upload your geographic data files for automatic analysis via WHISP API
**Talk to Reports:** Select Ecuador or Guatemala for country-specific EUDR analysis
## β Best Practices
- Be specific about regions, commodities, or time periods
- Ask one question at a time for clearer answers
- Use follow-up questions to explore topics deeper
- Provide context when possible
""")
# About Tab
with gr.Tab("About"):
gr.Markdown("""
## About EUDR Q&A
The **EU Deforestation Regulation (EUDR)** requires companies to ensure that specific commodities
placed on the EU market are deforestation-free and legally produced.
This AI-powered tool helps stakeholders:
- Understand EUDR compliance requirements
- Analyze geographic deforestation data using WHISP API
- Assess supply chain risks
- Navigate complex regulatory landscapes
**Developed by GIZ** to enhance accessibility and understanding of EUDR requirements
through advanced AI and geographic data processing capabilities.
### Key Features:
- Automatic analysis of uploaded GeoJSON files via WHISP API
- Country-specific EUDR compliance guidance
- Real-time question answering with source citations
- User-friendly interface for complex regulatory information
""")
# Disclaimer Tab
with gr.Tab("Disclaimer"):
gr.Markdown("""
## Important Disclaimers
β οΈ **Scope & Limitations:**
- This tool is designed for EUDR compliance assistance and geographic data analysis
- Responses should not be considered official legal or compliance advice
- Always consult qualified professionals for official compliance decisions
β οΈ **Data & Privacy:**
- Uploaded GeoJSON files are processed via external WHISP API for analysis
- We collect usage statistics to improve the tool
- Files are processed temporarily and not permanently stored
β οΈ **AI Limitations:**
- Responses are AI-generated and may contain inaccuracies
- The tool is a prototype under continuous development
- Always verify important information with authoritative sources
**Data Collection:** We collect questions, answers, feedback, and anonymized usage statistics
to improve tool performance based on legitimate interest in service enhancement.
By using this tool, you acknowledge these limitations and agree to use responses responsibly.
""")
# Event Handlers
# Toggle search method
search_method.change(
fn=toggle_search_method,
inputs=[search_method],
outputs=[geojson_section, reports_section, dropdown_country]
)
# File upload - automatically analyze and display in chat
uploaded_file.change(
fn=auto_analyze_file,
inputs=[uploaded_file, chatbot],
outputs=[chatbot, examples_hidden],
queue=False
).then(
fn=auto_display_results,
inputs=[examples_hidden, chatbot, uploaded_file],
outputs=[chatbot, sources_textbox]
)
# Chat functionality
textbox.submit(
start_chat,
[textbox, chatbot],
[textbox, tabs, chatbot],
queue=False
).then(
chat_response,
[textbox, chatbot, search_method, dropdown_country, uploaded_file],
[chatbot, sources_textbox]
).then(
lambda: gr.update(visible=True),
outputs=[feedback_row]
).then(
finish_chat,
outputs=[textbox]
)
# Examples functionality
examples_hidden.change(
start_chat,
[examples_hidden, chatbot],
[textbox, tabs, chatbot],
queue=False
).then(
chat_response,
[examples_hidden, chatbot, search_method, dropdown_country, uploaded_file],
[chatbot, sources_textbox]
).then(
lambda: gr.update(visible=True),
outputs=[feedback_row]
).then(
finish_chat,
outputs=[textbox]
)
# Sample questions dropdown
dropdown_samples.change(
change_sample_questions,
[dropdown_samples],
sample_groups
)
# Feedback buttons
okay_btn.click(
lambda: (gr.update(visible=False), gr.update(visible=True)),
outputs=[feedback_row, feedback_thanks]
)
not_okay_btn.click(
lambda: (gr.update(visible=False), gr.update(visible=True)),
outputs=[feedback_row, feedback_thanks]
)
# Launch the app
if __name__ == "__main__":
demo.launch() |