Skip to content

Instantly share code, notes, and snippets.

@thuwarakeshm
Last active June 29, 2022 15:17
Show Gist options
  • Save thuwarakeshm/54ea907ab5d6f296499df1cc4d199f30 to your computer and use it in GitHub Desktop.
Save thuwarakeshm/54ea907ab5d6f296499df1cc4d199f30 to your computer and use it in GitHub Desktop.
Gradio demo
def find_palindroms(text: str) -> str:
# A placeholder for the palindromes found
palindromes = []
# loop through all the words in the text
for word in text.split():
# and check if they are palindromes
if word == word[::-1]:
# if they are, add them to the list
palindromes.append(word)
# Return the list of palindromes as a string
return "|".join(palindromes)
# 1. Import Gradio
import gradio as gr
# 2. Create a Gradio interface with prefered input and output widgets
app = gr.Interface(find_palindroms, inputs=["text"], outputs=["text"])
# 3. Launch the app. Bingo!
app.launch()
from email.policy import default
from nltk.corpus import stopwords
# Add more inputs to the find_palindroms function.
def find_palindroms(text: str, remove_stops: bool, max_output: int, lang: str) -> str:
palindromes = []
if remove_stops:
stops = set(stopwords.words(lang))
words = [word for word in text.split() if word not in stops]
else:
words = text.split()
for word in words:
if word == word[::-1]:
palindromes.append(word)
if len(palindromes) > max_output:
palindromes = palindromes[:max_output]
return "|".join(palindromes)
import gradio as gr
# Create more widgets in the Gradio app.
app = gr.Interface(
find_palindroms,
inputs=[
gr.Textbox(label="Text", value=""),
gr.Checkbox(label="Remove stopwords", value=False),
gr.Slider(label="Max output", value=3, minimum=1, maximum=10),
gr.Radio(choices=["english", "french", "spanish"], label="Language"),
],
outputs=["text"],
)
app.launch()
import pickle
import gradio as gr
"""
# You can use this block to train and save a model.
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Load the data
data = pd.read_csv("data.csv")
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
data[["temp", "humidity"]], data["rainfall"], test_size=0.2, random_state=42
)
# Train the model
model = LinearRegression().fit(X_train, y_train)
# Save the model
pickle.dump(model, open("model.pkl", "wb"))
"""
def predict_rainfall(temp, humidity):
"""
Predict the rainfall based on the temperature and humidity.
This function uses a pickled scikitlearn model to predict the rainfall.
The output is a string with the predicted rainfall.
"""
# Load the model
model = pickle.load(open("model.pkl", "rb"))
# Predict the rainfall
rainfall = model.predict([[temp, humidity]])
# Return the rainfall
return rainfall
app = gr.Interface(
predict_rainfall,
inputs=[
gr.Number(value=20, label="Temperature"),
gr.Number(value=50, label="Humidity"),
],
outputs=[gr.Text(label="Rainfall")],
)
app.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment