diff --git a/demo/calculator.py b/demo/calculator.py index de95d97faa..b34bb8fb80 100644 --- a/demo/calculator.py +++ b/demo/calculator.py @@ -21,5 +21,6 @@ iface = gr.Interface(calculator, [0, "subtract", 1.2], ] ) + if __name__ == "__main__": - iface.launch() \ No newline at end of file + iface.launch() diff --git a/demo/diff_texts.py b/demo/diff_texts.py index 8a05b5ed91..4501aa8731 100644 --- a/demo/diff_texts.py +++ b/demo/diff_texts.py @@ -11,7 +11,7 @@ def diff_texts(text1, text2): ] -io = gr.Interface( +iface = gr.Interface( diff_texts, [ gr.inputs.Textbox(lines=3, default="The quick brown fox jumped over the lazy dogs."), @@ -23,7 +23,7 @@ io = gr.Interface( " ": "none", })) -io.test_launch() +iface.test_launch() if __name__ == "__main__": io.launch() diff --git a/demo/digit_classifier.py b/demo/digit_classifier.py index 2b0ed44bc3..5e58fa12ba 100644 --- a/demo/digit_classifier.py +++ b/demo/digit_classifier.py @@ -15,7 +15,7 @@ def recognize_digit(image): return {str(i): prediction[i] for i in range(10)} -io = gr.Interface( +iface = gr.Interface( recognize_digit, "sketchpad", gradio.outputs.Label(num_top_classes=3), @@ -23,5 +23,8 @@ io = gr.Interface( capture_session=True, ) -io.test_launch() -io.launch() +iface.test_launch() + +if __name__ == "__main__": + iface.launch() + diff --git a/demo/face_segment.py b/demo/face_segment.py index 72a0a6d42d..272267d582 100644 --- a/demo/face_segment.py +++ b/demo/face_segment.py @@ -34,5 +34,6 @@ def segment_face(inp): iface = gr.Interface(segment_face, "webcam", "image", capture_session=True) + if __name__ == "__main__": iface.launch() \ No newline at end of file diff --git a/demo/filter_records.py b/demo/filter_records.py index 6a996fef65..a947952fc1 100644 --- a/demo/filter_records.py +++ b/demo/filter_records.py @@ -7,7 +7,7 @@ import random def filter_records(records, gender): return records[records['gender'] == gender] -io = gr.Interface(filter_records, +iface = gr.Interface(filter_records, [ gr.inputs.Dataframe(headers=["name", "age", "gender"], datatype=["str", "number", "str"], row_count=5), gr.inputs.Dropdown(["M", "F", "O"]) @@ -16,5 +16,8 @@ io = gr.Interface(filter_records, description="Enter gender as 'M', 'F', or 'O' for other." ) -io.test_launch() -io.launch() +iface.test_launch() + +if __name__ == "__main__": + iface.launch() + diff --git a/demo/form_graph.py b/demo/form_graph.py index 6d6666a9d7..0d1364fdec 100644 --- a/demo/form_graph.py +++ b/demo/form_graph.py @@ -22,7 +22,7 @@ def plot_forecast(final_year, companies, noise, show_legend, point_style): return fig -gr.Interface(plot_forecast, +iface = gr.Interface(plot_forecast, [ gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"), @@ -33,4 +33,7 @@ gr.Interface(plot_forecast, gr.inputs.Dropdown(["cross", "line", "circle"], label="Style"), ], gr.outputs.Image(plot=True, label="forecast") - ).launch() + ) + +if __name__ == "__main__": + iface.launch() diff --git a/demo/generate_tone.py b/demo/generate_tone.py index a2067efaee..19540cd4ca 100644 --- a/demo/generate_tone.py +++ b/demo/generate_tone.py @@ -16,7 +16,7 @@ def generate_tone(note, octave, duration): return sr, audio -io = gr.Interface( +iface = gr.Interface( generate_tone, [ gr.inputs.Dropdown(notes, type="index"), @@ -24,5 +24,6 @@ io = gr.Interface( gr.inputs.Textbox(type="number", default=1, label="Duration in seconds") ], "audio") -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/image_classifier.py b/demo/image_classifier.py index 513d63d6a0..a8453cb141 100644 --- a/demo/image_classifier.py +++ b/demo/image_classifier.py @@ -8,9 +8,11 @@ from PIL import Image import requests from urllib.request import urlretrieve import json +import os # Load human-readable labels for ImageNet. -with open("files/imagenet_labels.json") as labels_file: +current_dir = os.path.dirname(os.path.realpath(__file__)) +with open(os.path.join(current_dir, "files/imagenet_labels.json")) as labels_file: labels = json.load(labels_file) mobile_net = tf.keras.applications.MobileNetV2() @@ -26,7 +28,7 @@ def image_classifier(im): image = gr.inputs.Image(shape=(224, 224)) label = gr.outputs.Label(num_top_classes=3) -io = gr.Interface(image_classifier, image, label, +iface = gr.Interface(image_classifier, image, label, capture_session=True, interpretation="default", examples=[ @@ -34,4 +36,5 @@ io = gr.Interface(image_classifier, image, label, ["images/lion.jpg"] ]) -io.launch() \ No newline at end of file +if __name__ == "__main__": + iface.launch() diff --git a/demo/image_mod.py b/demo/image_mod.py index cf00817a1f..33d78b6ba0 100644 --- a/demo/image_mod.py +++ b/demo/image_mod.py @@ -7,7 +7,7 @@ def image_mod(image): return image.rotate(45) -io = gr.Interface(image_mod, +iface = gr.Interface(image_mod, gr.inputs.Image(type="pil"), "image", examples=[ @@ -16,7 +16,7 @@ io = gr.Interface(image_mod, ["images/lion.jpg"], ]) -io.test_launch() +iface.test_launch() if __name__ == "__main__": - io.launch() + iface.launch() diff --git a/demo/longest_word.py b/demo/longest_word.py index 34a2db57d1..47ce431f85 100644 --- a/demo/longest_word.py +++ b/demo/longest_word.py @@ -9,10 +9,10 @@ def longest_word(text): ex = "The quick brown fox jumped over the lazy dog." -io = gr.Interface(longest_word, "textbox", "label", +iface = gr.Interface(longest_word, "textbox", "label", interpretation="default", examples=[[ex]]) -io.test_launch() +iface.test_launch() if __name__ == "__main__": - io.launch() + iface.launch() diff --git a/demo/main_note.py b/demo/main_note.py index 1a51e84ed8..4f0c491848 100644 --- a/demo/main_note.py +++ b/demo/main_note.py @@ -37,7 +37,7 @@ def main_note(audio): volume_per_pitch[pitch] += 1.0 * volume / total_volume return volume_per_pitch -io = gr.Interface( +iface = gr.Interface( main_note, "microphone", gr.outputs.Label(num_top_classes=4), @@ -47,4 +47,5 @@ io = gr.Interface( ], interpretation="default") -io.launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/matrix_transpose.py b/demo/matrix_transpose.py index b37df57d6c..b5327f5a15 100644 --- a/demo/matrix_transpose.py +++ b/demo/matrix_transpose.py @@ -7,7 +7,7 @@ def transpose(matrix): return matrix.T -io = gr.Interface( +iface = gr.Interface( transpose, gr.inputs.Dataframe(type="numpy", datatype="number", row_count=5, col_count=3), "numpy", @@ -20,7 +20,7 @@ io = gr.Interface( ] ) -io.test_launch() +iface.test_launch() if __name__ == "__main__": - io.launch() + iface.launch() diff --git a/demo/outbreak_forecast.py b/demo/outbreak_forecast.py index c89edc63df..a2852c7d44 100644 --- a/demo/outbreak_forecast.py +++ b/demo/outbreak_forecast.py @@ -32,4 +32,4 @@ iface = gr.Interface(outbreak, ], "plot") if __name__ == "__main__": - iface.launch() \ No newline at end of file + iface.launch() diff --git a/demo/question_answer.py b/demo/question_answer.py index d891367c36..61a87a1f4c 100644 --- a/demo/question_answer.py +++ b/demo/question_answer.py @@ -15,4 +15,4 @@ iface = gr.Interface(qa_func, ], gr.outputs.Textbox(label="Answer")) if __name__ == "__main__": - iface.launch() \ No newline at end of file + iface.launch() diff --git a/demo/reverse_audio.py b/demo/reverse_audio.py index 54a08b5c5b..559f253bc7 100644 --- a/demo/reverse_audio.py +++ b/demo/reverse_audio.py @@ -9,7 +9,8 @@ def reverse_audio(audio): return (sr, np.flipud(data)) -io = gr.Interface(reverse_audio, "microphone", "audio") +iface = gr.Interface(reverse_audio, "microphone", "audio") -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/sentence_builder.py b/demo/sentence_builder.py index 6b255d59ef..0d52de2627 100644 --- a/demo/sentence_builder.py +++ b/demo/sentence_builder.py @@ -6,7 +6,7 @@ def sentence_builder(quantity, animal, place, activity_list, morning): return f"""The {quantity} {animal}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}""" -io = gr.Interface( +iface = gr.Interface( sentence_builder, [ gr.inputs.Slider(2, 20), @@ -23,7 +23,7 @@ io = gr.Interface( [8, "cat", "zoo", ["ate"], True], ]) -io.test_launch() +iface.test_launch() if __name__ == "__main__": - io.launch() + iface.launch() diff --git a/demo/sentiment_analysis.py b/demo/sentiment_analysis.py index b7a869c3dc..096a15b6ed 100644 --- a/demo/sentiment_analysis.py +++ b/demo/sentiment_analysis.py @@ -11,7 +11,8 @@ def sentiment_analysis(text): del scores["compound"] return scores -io = gr.Interface(sentiment_analysis, "textbox", "label", interpretation="default") +iface = gr.Interface(sentiment_analysis, "textbox", "label", interpretation="default") -io.test_launch() -io.launch() \ No newline at end of file +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/spectogram.py b/demo/spectogram.py index ae348ff3ec..ec8c6f480a 100644 --- a/demo/spectogram.py +++ b/demo/spectogram.py @@ -15,7 +15,8 @@ def spectrogram(audio): return plt -io = gr.Interface(spectrogram, "audio", "plot") +iface = gr.Interface(spectrogram, "audio", "plot") -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/stock_forecast.py b/demo/stock_forecast.py index 55403bb3c3..a55a6e5596 100644 --- a/demo/stock_forecast.py +++ b/demo/stock_forecast.py @@ -23,7 +23,7 @@ def stock_forecast(final_year, companies, noise, show_legend, point_style): return fig -io = gr.Interface( +iface = gr.Interface( stock_forecast, [ gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"), @@ -33,5 +33,6 @@ io = gr.Interface( gr.inputs.Dropdown(["cross", "line", "circle"], label="Style")], gr.outputs.Image(plot=True, label="forecast")) -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/tax_calculator.py b/demo/tax_calculator.py index 5d0dc49086..020ab25de2 100644 --- a/demo/tax_calculator.py +++ b/demo/tax_calculator.py @@ -25,7 +25,7 @@ def tax_calculator(income, marital_status, assets): return round(total_tax) -io = gr.Interface( +iface = gr.Interface( tax_calculator, [ "number", @@ -44,4 +44,5 @@ io = gr.Interface( ] ) -io.launch() \ No newline at end of file +if __name__ == "__main__": + iface.launch() diff --git a/demo/text_analysis.py b/demo/text_analysis.py index 19e0c0ca32..44ae0553de 100644 --- a/demo/text_analysis.py +++ b/demo/text_analysis.py @@ -23,7 +23,7 @@ def text_analysis(text): return pos_tokens, pos_count, html -io = gr.Interface( +iface = gr.Interface( text_analysis, gr.inputs.Textbox(placeholder="Enter sentence here..."), [ @@ -35,5 +35,6 @@ io = gr.Interface( ] ) -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/titanic_survival.py b/demo/titanic_survival.py index ce6fa270da..25c31a8930 100644 --- a/demo/titanic_survival.py +++ b/demo/titanic_survival.py @@ -8,8 +8,10 @@ from sklearn import preprocessing from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score +import os -data = pd.read_csv('files/titanic.csv') +current_dir = os.path.dirname(os.path.realpath(__file__)) +data = pd.read_csv(os.path.join(current_dir, 'files/titanic.csv')) def encode_age(df): df.Age = df.Age.fillna(-0.5) @@ -66,7 +68,7 @@ def predict_survival(passenger_class, is_male, age, company, fare, embark_point) pred = clf.predict_proba(df)[0] return {'Perishes': pred[0], 'Survives': pred[1]} -io = gr.Interface( +iface = gr.Interface( predict_survival, [ gr.inputs.Dropdown(["first", "second", "third"], type="index"), @@ -85,4 +87,5 @@ io = gr.Interface( interpretation="default" ) -io.launch() \ No newline at end of file +if __name__ == "__main__": + iface.launch() diff --git a/demo/webcam.py b/demo/webcam.py index 1c17aa631d..1632cd516a 100644 --- a/demo/webcam.py +++ b/demo/webcam.py @@ -8,7 +8,8 @@ def snap(image): return np.flipud(image) -io = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image") +iface = gr.Interface(snap, gr.inputs.Image(shape=(100,100), image_mode="L", source="webcam"), "image") -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/zip_to_json.py b/demo/zip_to_json.py index b457c50cf8..7ef9c05d80 100644 --- a/demo/zip_to_json.py +++ b/demo/zip_to_json.py @@ -16,7 +16,8 @@ def zip_to_json(file_obj): return files -io = gr.Interface(zip_to_json, "file", "json") +iface = gr.Interface(zip_to_json, "file", "json") -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch() diff --git a/demo/zip_two_files.py b/demo/zip_two_files.py index cb1fb0fb82..c2ed9e01f0 100644 --- a/demo/zip_two_files.py +++ b/demo/zip_two_files.py @@ -11,7 +11,7 @@ def zip_two_files(file1, file2): return "tmp.zip" -io = gr.Interface( +iface = gr.Interface( zip_two_files, ["file", "file"], "file", @@ -21,5 +21,6 @@ io = gr.Interface( ] ) -io.test_launch() -io.launch() +iface.test_launch() +if __name__ == "__main__": + iface.launch()