Spaces:
Running
on
Zero
Running
on
Zero
jedick
commited on
Commit
·
bf82de1
1
Parent(s):
9d76733
Add Loading Data textbox
Browse files
app.py
CHANGED
|
@@ -219,7 +219,7 @@ def to_workflow(request: gr.Request, *args):
|
|
| 219 |
yield value
|
| 220 |
|
| 221 |
|
| 222 |
-
@spaces.GPU(duration=
|
| 223 |
def run_workflow_local(*args):
|
| 224 |
for value in run_workflow(*args):
|
| 225 |
yield value
|
|
@@ -273,19 +273,26 @@ with gr.Blocks(
|
|
| 273 |
render=False,
|
| 274 |
)
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
downloading = gr.Textbox(
|
| 277 |
-
|
| 278 |
-
label="Downloading Data
|
| 279 |
visible=False,
|
| 280 |
render=False,
|
| 281 |
)
|
| 282 |
extracting = gr.Textbox(
|
| 283 |
-
|
| 284 |
-
label="Extracting Data
|
| 285 |
visible=False,
|
| 286 |
render=False,
|
| 287 |
)
|
| 288 |
-
|
| 289 |
value="Email database is missing. Try reloading this page. If the problem persists, please contact the maintainer.",
|
| 290 |
lines=1,
|
| 291 |
label="Error downloading or extracting data",
|
|
@@ -442,9 +449,10 @@ with gr.Blocks(
|
|
| 442 |
chatbot.render()
|
| 443 |
input.render()
|
| 444 |
# Render textboxes for data loading progress
|
|
|
|
| 445 |
downloading.render()
|
| 446 |
extracting.render()
|
| 447 |
-
|
| 448 |
# Right column: Info, Examples
|
| 449 |
with gr.Column(scale=1):
|
| 450 |
status = gr.Markdown(get_status_text(compute_mode.value))
|
|
@@ -611,21 +619,18 @@ with gr.Blocks(
|
|
| 611 |
def download():
|
| 612 |
"""Download the application data"""
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
|
| 617 |
-
|
| 618 |
-
|
| 619 |
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
| 620 |
aws_secret_access_key=os.getenv("AWS_ACCESS_KEY_SECRET"),
|
| 621 |
-
region_name=
|
| 622 |
)
|
| 623 |
-
|
| 624 |
-
def download_file_from_bucket(bucket_name, s3_key, dst_path):
|
| 625 |
-
session = aws_session()
|
| 626 |
s3_resource = session.resource("s3")
|
| 627 |
bucket = s3_resource.Bucket(bucket_name)
|
| 628 |
-
bucket.download_file(Key=s3_key, Filename=
|
| 629 |
|
| 630 |
def download_dropbox_file(shared_url, output_file):
|
| 631 |
"""Download file from Dropbox"""
|
|
@@ -649,16 +654,13 @@ with gr.Blocks(
|
|
| 649 |
f"Failed to download file. HTTP Status Code: {response.status_code}"
|
| 650 |
)
|
| 651 |
|
| 652 |
-
if not os.path.isdir(db_dir):
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
# download_dropbox_file(shared_link, output_filename)
|
| 660 |
-
|
| 661 |
-
return None
|
| 662 |
|
| 663 |
def extract():
|
| 664 |
"""Extract the db.zip file"""
|
|
@@ -697,51 +699,46 @@ with gr.Blocks(
|
|
| 697 |
except Exception as e:
|
| 698 |
print(f"An error occurred: {e}")
|
| 699 |
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
return os.path.isdir(db_dir)
|
| 706 |
-
|
| 707 |
-
def is_data_missing():
|
| 708 |
-
"""Check if the database directory is missing"""
|
| 709 |
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
| 711 |
|
| 712 |
false = gr.State(False)
|
| 713 |
-
|
| 714 |
-
have_data = gr.State()
|
| 715 |
|
| 716 |
-
# When app is launched:
|
| 717 |
-
# if necessary, make chat interface visible
|
| 718 |
-
#
|
| 719 |
|
| 720 |
# fmt: off
|
| 721 |
demo.load(
|
| 722 |
-
|
| 723 |
).then(
|
| 724 |
-
|
| 725 |
).then(
|
| 726 |
download, None, [downloading], api_name=False
|
| 727 |
).then(
|
| 728 |
change_visibility, [false], [downloading], api_name=False
|
| 729 |
).then(
|
| 730 |
-
|
| 731 |
).then(
|
| 732 |
extract, None, [extracting], api_name=False
|
| 733 |
).then(
|
| 734 |
change_visibility, [false], [extracting], api_name=False
|
| 735 |
).then(
|
| 736 |
-
|
| 737 |
).then(
|
| 738 |
-
|
| 739 |
).then(
|
| 740 |
-
|
| 741 |
-
).then(
|
| 742 |
-
is_data_missing, None, [need_data], api_name=False
|
| 743 |
).then(
|
| 744 |
-
|
| 745 |
)
|
| 746 |
# fmt: on
|
| 747 |
|
|
|
|
| 219 |
yield value
|
| 220 |
|
| 221 |
|
| 222 |
+
@spaces.GPU(duration=100)
|
| 223 |
def run_workflow_local(*args):
|
| 224 |
for value in run_workflow(*args):
|
| 225 |
yield value
|
|
|
|
| 273 |
render=False,
|
| 274 |
)
|
| 275 |
|
| 276 |
+
loading_data = gr.Textbox(
|
| 277 |
+
"Please wait for data loading to complete.",
|
| 278 |
+
max_lines=0,
|
| 279 |
+
label="Loading Data",
|
| 280 |
+
visible=False,
|
| 281 |
+
render=False,
|
| 282 |
+
)
|
| 283 |
downloading = gr.Textbox(
|
| 284 |
+
max_lines=1,
|
| 285 |
+
label="Downloading Data",
|
| 286 |
visible=False,
|
| 287 |
render=False,
|
| 288 |
)
|
| 289 |
extracting = gr.Textbox(
|
| 290 |
+
max_lines=1,
|
| 291 |
+
label="Extracting Data",
|
| 292 |
visible=False,
|
| 293 |
render=False,
|
| 294 |
)
|
| 295 |
+
missing_data = gr.Textbox(
|
| 296 |
value="Email database is missing. Try reloading this page. If the problem persists, please contact the maintainer.",
|
| 297 |
lines=1,
|
| 298 |
label="Error downloading or extracting data",
|
|
|
|
| 449 |
chatbot.render()
|
| 450 |
input.render()
|
| 451 |
# Render textboxes for data loading progress
|
| 452 |
+
loading_data.render()
|
| 453 |
downloading.render()
|
| 454 |
extracting.render()
|
| 455 |
+
missing_data.render()
|
| 456 |
# Right column: Info, Examples
|
| 457 |
with gr.Column(scale=1):
|
| 458 |
status = gr.Markdown(get_status_text(compute_mode.value))
|
|
|
|
| 619 |
def download():
|
| 620 |
"""Download the application data"""
|
| 621 |
|
| 622 |
+
def download_file_from_bucket(bucket_name, s3_key, output_file):
|
| 623 |
+
"""Download file from S3 bucket"""
|
| 624 |
|
| 625 |
+
# https://thecodinginterface.com/blog/aws-s3-python-boto3
|
| 626 |
+
session = boto3.session.Session(
|
| 627 |
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
|
| 628 |
aws_secret_access_key=os.getenv("AWS_ACCESS_KEY_SECRET"),
|
| 629 |
+
region_name="us-east-1",
|
| 630 |
)
|
|
|
|
|
|
|
|
|
|
| 631 |
s3_resource = session.resource("s3")
|
| 632 |
bucket = s3_resource.Bucket(bucket_name)
|
| 633 |
+
bucket.download_file(Key=s3_key, Filename=output_file)
|
| 634 |
|
| 635 |
def download_dropbox_file(shared_url, output_file):
|
| 636 |
"""Download file from Dropbox"""
|
|
|
|
| 654 |
f"Failed to download file. HTTP Status Code: {response.status_code}"
|
| 655 |
)
|
| 656 |
|
| 657 |
+
if not os.path.isdir(db_dir) and not os.path.exists("db.zip"):
|
| 658 |
+
# For S3 (need AWS_ACCESS_KEY_ID and AWS_ACCESS_KEY_SECRET)
|
| 659 |
+
download_file_from_bucket("r-help-chat", "db.zip", "db.zip")
|
| 660 |
+
## For Dropbox (shared file - key is in URL)
|
| 661 |
+
# shared_link = "https://www.dropbox.com/scl/fi/jx90g5lorpgkkyyzeurtc/db.zip?rlkey=wvqa3p9hdy4rmod1r8yf2am09&st=l9tsam56&dl=0"
|
| 662 |
+
# output_filename = "db.zip"
|
| 663 |
+
# download_dropbox_file(shared_link, output_filename)
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
def extract():
|
| 666 |
"""Extract the db.zip file"""
|
|
|
|
| 699 |
except Exception as e:
|
| 700 |
print(f"An error occurred: {e}")
|
| 701 |
|
| 702 |
+
def visible_if_data_present():
|
| 703 |
+
"""Make component visible if the database directory is present"""
|
| 704 |
+
visible = os.path.isdir(db_dir)
|
| 705 |
+
return change_visibility(visible)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 706 |
|
| 707 |
+
def visible_if_data_missing():
|
| 708 |
+
"""Make component visible if the database directory is missing"""
|
| 709 |
+
visible = not os.path.isdir(db_dir)
|
| 710 |
+
return change_visibility(visible)
|
| 711 |
|
| 712 |
false = gr.State(False)
|
| 713 |
+
true = gr.State(True)
|
|
|
|
| 714 |
|
| 715 |
+
# When app is launched: show "Loading Data" textbox, download and extract
|
| 716 |
+
# data if necessary, make chat interface visible or show error textbox, and
|
| 717 |
+
# update database info
|
| 718 |
|
| 719 |
# fmt: off
|
| 720 |
demo.load(
|
| 721 |
+
change_visibility, [true], [loading_data], api_name=False
|
| 722 |
).then(
|
| 723 |
+
visible_if_data_missing, None, [downloading], api_name=False
|
| 724 |
).then(
|
| 725 |
download, None, [downloading], api_name=False
|
| 726 |
).then(
|
| 727 |
change_visibility, [false], [downloading], api_name=False
|
| 728 |
).then(
|
| 729 |
+
visible_if_data_missing, None, [extracting], api_name=False
|
| 730 |
).then(
|
| 731 |
extract, None, [extracting], api_name=False
|
| 732 |
).then(
|
| 733 |
change_visibility, [false], [extracting], api_name=False
|
| 734 |
).then(
|
| 735 |
+
change_visibility, [false], [loading_data], api_name=False
|
| 736 |
).then(
|
| 737 |
+
visible_if_data_present, None, [chat_interface], api_name=False
|
| 738 |
).then(
|
| 739 |
+
visible_if_data_missing, None, [missing_data], api_name=False
|
|
|
|
|
|
|
| 740 |
).then(
|
| 741 |
+
get_info_text, None, [info], api_name=False
|
| 742 |
)
|
| 743 |
# fmt: on
|
| 744 |
|