Anushree1 commited on
Commit
55cf8ba
·
verified ·
1 Parent(s): 3fbc0d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -32
app.py CHANGED
@@ -1,38 +1,24 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
- import numpy as np
4
- from tensorflow.keras.models import load_model
5
- import string
6
 
7
- # Load the trained model
8
- model = load_model('sign_language_model(1).h5')
 
 
9
 
10
- # Class names for prediction
11
- class_names = list(string.ascii_lowercase[:26].replace('j', '').replace('z', ''))
12
-
13
- # Function to predict the sign language from the image
14
  def predict(image):
15
- # Preprocess the input image
16
- image = np.array(image).astype(np.float32) / 255.0 # Normalize
17
- image = np.expand_dims(image, axis=-1) # Add channel dimension
18
- image = np.expand_dims(image, axis=0) # Add batch dimension
19
-
20
- # Predict using the trained model
21
- predictions = model.predict(image)
22
- predicted_class = np.argmax(predictions, axis=1)[0]
23
-
24
- # Return the predicted class
25
- return class_names[predicted_class]
26
 
27
- # Define Gradio interface
28
- iface = gr.Interface(
29
- fn=predict,
30
- inputs=gr.Image(shape=(28, 28), image_mode="L", source="upload"),
31
- outputs="text",
32
- live=True,
33
- title="Sign Language Recognition",
34
- description="Upload a sign language image, and the model will predict the corresponding letter."
35
- )
36
 
37
- # Launch the Gradio interface
38
- iface.launch()
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForImageClassification, AutoFeatureExtractor
 
 
4
 
5
+ # Load model and feature extractor (adjust these according to your model)
6
+ model_name = "sign_language_model(1).h5" # Replace with your model path or Hugging Face model
7
+ model = AutoModelForImageClassification.from_pretrained(model_name)
8
+ feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
9
 
10
+ # Define prediction function
 
 
 
11
  def predict(image):
12
+ inputs = feature_extractor(images=image, return_tensors="pt")
13
+ with torch.no_grad():
14
+ logits = model(**inputs).logits
15
+ predicted_class = logits.argmax(-1).item()
16
+ return predicted_class
 
 
 
 
 
 
17
 
18
+ # Set up the Gradio interface
19
+ interface = gr.Interface(fn=predict,
20
+ inputs=gr.Image(type="pil"),
21
+ outputs=gr.Text(label="Predicted Sign Language"))
 
 
 
 
 
22
 
23
+ if __name__ == "__main__":
24
+ interface.launch()