diamantrsd commited on
Commit
fd0b853
1 Parent(s): 62ca097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import tensorflow as tf
3
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
  import numpy as np
 
5
 
6
  # Load the image classification model
7
  image_classification_model = tf.keras.models.load_model("klasifikasi_pt1.h5")
@@ -11,13 +12,18 @@ gpt2_model_name = "diamantrsd/cerpen-generator-v3"
11
  gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_model_name)
12
  gpt2_tokenizer = GPT2Tokenizer.from_pretrained(gpt2_model_name)
13
 
 
 
14
  def classify_and_generate_text(image):
15
  try:
16
  # Convert Gradio Image interface output to a NumPy array
17
  img_array = image.astype('float32') / 255.0
18
 
19
- # Classify the image using the image classification model
20
- class_label = image_classification_model.predict(np.expand_dims(img_array, axis=0))
 
 
 
21
 
22
  # Map class label to corresponding category (adjust as needed)
23
  category = map_class_label_to_category(class_label)
@@ -29,6 +35,7 @@ def classify_and_generate_text(image):
29
  except Exception as e:
30
  return f"Error: {str(e)}"
31
 
 
32
  def map_class_label_to_category(class_label):
33
  # Map the class label to a category (replace with your own mapping)
34
  categories = ['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
@@ -41,7 +48,7 @@ def generate_text_with_gpt2(product_category):
41
  input_ids = gpt2_tokenizer.encode(prompt, return_tensors="pt")
42
 
43
  # Adjust parameters as needed
44
- max_length = 1000
45
  no_repeat_ngram_size = 3
46
  top_k = 50
47
  top_p = 0.95
 
2
  import tensorflow as tf
3
  from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
  import numpy as np
5
+ from tensorflow.image import resize
6
 
7
  # Load the image classification model
8
  image_classification_model = tf.keras.models.load_model("klasifikasi_pt1.h5")
 
12
  gpt2_model = GPT2LMHeadModel.from_pretrained(gpt2_model_name)
13
  gpt2_tokenizer = GPT2Tokenizer.from_pretrained(gpt2_model_name)
14
 
15
+
16
+
17
  def classify_and_generate_text(image):
18
  try:
19
  # Convert Gradio Image interface output to a NumPy array
20
  img_array = image.astype('float32') / 255.0
21
 
22
+ # Resize the image to the expected shape (224, 224)
23
+ img_array_resized = resize(img_array, (224, 224))
24
+
25
+ # Classify the resized image using the image classification model
26
+ class_label = image_classification_model.predict(np.expand_dims(img_array_resized, axis=0))
27
 
28
  # Map class label to corresponding category (adjust as needed)
29
  category = map_class_label_to_category(class_label)
 
35
  except Exception as e:
36
  return f"Error: {str(e)}"
37
 
38
+
39
  def map_class_label_to_category(class_label):
40
  # Map the class label to a category (replace with your own mapping)
41
  categories = ['Blazer', 'Blouse', 'Cardigan', 'Dress', 'Jacket',
 
48
  input_ids = gpt2_tokenizer.encode(prompt, return_tensors="pt")
49
 
50
  # Adjust parameters as needed
51
+ max_length = 50
52
  no_repeat_ngram_size = 3
53
  top_k = 50
54
  top_p = 0.95