|
import json |
|
import gradio as gr |
|
import pandas as pd |
|
from pyspark.sql import SparkSession |
|
|
|
|
|
spark = ( |
|
SparkSession |
|
.builder |
|
.master("local[*]") |
|
.appName("HF Spark Demo") |
|
.getOrCreate() |
|
) |
|
|
|
|
|
def count_words(text: str) -> str: |
|
df = spark.createDataFrame([(text,)], ["sentence"]) |
|
result = df.selectExpr("size(split(sentence, ' ')) as word_count").collect()[0] |
|
return f"Your input has {result['word_count']} words." |
|
|
|
|
|
def load_example_json() -> pd.DataFrame: |
|
|
|
df = spark.read.json("example_data.json") |
|
|
|
return df.toPandas() |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## π₯ Spark + Gradio Demo on 0.0.0.0") |
|
|
|
with gr.Tab("Word Count"): |
|
txt = gr.Textbox(lines=3, placeholder="Type something here...", label="Input Text") |
|
out = gr.Textbox(label="Word Count Result") |
|
txt.submit(count_words, txt, out) |
|
gr.Button("Count Words").click(count_words, txt, out) |
|
|
|
with gr.Tab("JSON Data Explorer"): |
|
df_table = gr.Dataframe( |
|
value=load_example_json(), |
|
label="Example Data", |
|
interactive=False |
|
) |
|
gr.Button("Reload Data").click(load_example_json, None, df_table) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
enable_queue=True |
|
) |
|
|