Spaces:
Runtime error
Runtime error
Commit
·
6c66e08
1
Parent(s):
c338b98
Upload 3 files
Browse files- README.md +0 -12
- app.py +80 -0
- requirements.txt +3 -0
README.md
CHANGED
@@ -1,12 +0,0 @@
|
|
1 |
-
---
|
2 |
-
title: Wine
|
3 |
-
emoji: 📚
|
4 |
-
colorFrom: indigo
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.1.2
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw, ImageFont
|
3 |
+
import requests
|
4 |
+
import hopsworks
|
5 |
+
import joblib
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
project = hopsworks.login()
|
9 |
+
fs = project.get_feature_store()
|
10 |
+
|
11 |
+
mr = project.get_model_registry()
|
12 |
+
model = mr.get_model("wine_model", version=1)
|
13 |
+
model_dir = model.download()
|
14 |
+
model = joblib.load(model_dir + "/wine_model.pkl")
|
15 |
+
print("Model downloaded")
|
16 |
+
|
17 |
+
|
18 |
+
def generate_number_image(number, img_size=(100, 100), bg_color='white', text_color='black'):
|
19 |
+
"""
|
20 |
+
Generate image of a given number.
|
21 |
+
Function generated by ChatGPT
|
22 |
+
"""
|
23 |
+
# Create an image with white background
|
24 |
+
image = Image.new('RGB', img_size, bg_color)
|
25 |
+
|
26 |
+
# Initialize the drawing context
|
27 |
+
draw = ImageDraw.Draw(image)
|
28 |
+
|
29 |
+
# Font settings (Default font in this case)
|
30 |
+
font = ImageFont.load_default()
|
31 |
+
|
32 |
+
# Calculate width and height of the text
|
33 |
+
text_width, text_height = draw.textsize(str(number), font)
|
34 |
+
|
35 |
+
# Calculate X, Y position of the text
|
36 |
+
x = (img_size[0] - text_width) / 2
|
37 |
+
y = (img_size[1] - text_height) / 2
|
38 |
+
|
39 |
+
# Draw the number on the image
|
40 |
+
draw.text((x, y), str(number), fill=text_color, font=font)
|
41 |
+
|
42 |
+
return image
|
43 |
+
|
44 |
+
def wine(fixed_acidity, citric_acid, type_white, chlorides, volatile_acidity, density, alcohol):
|
45 |
+
print("Calling function")
|
46 |
+
# df = pd.DataFrame([[sepal_length],[sepal_width],[petal_length],[petal_width]],
|
47 |
+
df = pd.DataFrame([[fixed_acidity, citric_acid, type_white, chlorides, volatile_acidity, density, alcohol]],
|
48 |
+
columns=['fixed_acidity', 'citric_acid', 'type_white', 'chlorides', 'volatile_acidity', 'density', 'alcohol'])
|
49 |
+
print("Predicting")
|
50 |
+
print(df)
|
51 |
+
# 'res' is a list of predictions returned as the label.
|
52 |
+
res = model.predict(df)
|
53 |
+
# We add '[0]' to the result of the transformed 'res', because 'res' is a list, and we only want
|
54 |
+
# the first element.
|
55 |
+
# print("Res: {0}").format(res)
|
56 |
+
print(res)
|
57 |
+
wine_url = generate_number_image(int(res))
|
58 |
+
img = Image.open(requests.get(wine_url, stream=True).raw)
|
59 |
+
return img
|
60 |
+
|
61 |
+
|
62 |
+
demo = gr.Interface(
|
63 |
+
fn=wine,
|
64 |
+
title="Wine Quality Predictive Analytics",
|
65 |
+
description="Experiment with fixed_acidity, citric_acid, type, chlorides, volatile_acidity, density, alcohol"
|
66 |
+
"to predict of which quality the wine is.",
|
67 |
+
allow_flagging="never",
|
68 |
+
inputs=[
|
69 |
+
gr.inputs.Number(default=7.2, label="fixed acidity"),
|
70 |
+
gr.inputs.Number(default=0.34, label="volatile acidity"),
|
71 |
+
gr.inputs.Number(default=0.32, label="citric acid"),
|
72 |
+
gr.inputs.Textbox(default="red", label="type (red, white)"),
|
73 |
+
gr.inputs.Number(default=10.5, label="alcohol"),
|
74 |
+
gr.inputs.Number(default=0.99, label="density"),
|
75 |
+
gr.inputs.Number(default=0.06, label="chlorides"),
|
76 |
+
],
|
77 |
+
outputs=gr.Image(type="pil"))
|
78 |
+
|
79 |
+
demo.launch(debug=True)
|
80 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
hopsworks
|
2 |
+
joblib
|
3 |
+
scikit-learn==1.1.1
|