Vinay15 commited on
Commit
f2460f7
·
verified ·
1 Parent(s): 21be1a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -1,23 +1,38 @@
1
- from flask import Flask, request, jsonify
2
- from transformers import AutoTokenizer, AutoModel
 
 
3
 
4
- app = Flask(__name__)
 
 
 
 
 
 
 
 
 
5
 
6
- # Load model and tokenizer
7
- try:
8
- tokenizer = AutoTokenizer.from_pretrained('stepfun-ai/GOT-OCR2_0', revision='cf6b7386bc89a54f09785612ba74cb12de6fa17c', trust_remote_code=True)
9
- model = AutoModel.from_pretrained('stepfun-ai/GOT-OCR2_0', revision='cf6b7386bc89a54f09785612ba74cb12de6fa17c', trust_remote_code=True)
10
- except Exception as e:
11
- print(f"Error loading model and tokenizer: {e}")
 
 
 
 
12
 
13
- @app.route('/predict', methods=['POST'])
14
- def predict():
15
- # Assuming you send image data in the request
16
- data = request.json
17
- # Add your model inference logic here
18
- # e.g., model.forward(data)
 
 
19
 
20
- return jsonify({"message": "Prediction made successfully!"})
21
-
22
- if __name__ == "__main__":
23
- app.run(host='0.0.0.0', port=5000) # Adjust port if necessary
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ # Assuming 'model' and 'tokenizer' are defined elsewhere in your code
4
+ # from your_model_file import model, tokenizer
5
 
6
+ def load_image(image_file):
7
+ """Load and preprocess the image."""
8
+ if isinstance(image_file, Image.Image): # Check if the input is an Image object
9
+ return image_file.convert("RGB") # Convert to RGB if necessary
10
+ elif isinstance(image_file, str) and (image_file.startswith('http') or image_file.startswith('https')):
11
+ # Handle URL case (you can use an external library to fetch the image if needed)
12
+ return Image.open(requests.get(image_file, stream=True).raw).convert("RGB")
13
+ else:
14
+ # Handle file path case
15
+ return Image.open(image_file).convert("RGB")
16
 
17
+ def perform_ocr(image):
18
+ """Perform OCR on the uploaded image."""
19
+ try:
20
+ # Load and preprocess the image
21
+ processed_image = load_image(image)
22
+ # Use the model for OCR
23
+ res = model.chat(tokenizer, processed_image, ocr_type='ocr')
24
+ return res
25
+ except Exception as e:
26
+ return str(e) # Return the error message
27
 
28
+ # Gradio interface setup
29
+ iface = gr.Interface(
30
+ fn=perform_ocr,
31
+ inputs=gr.Image(type="pil"), # Ensure Gradio accepts images as PIL images
32
+ outputs="text",
33
+ title="OCR Application",
34
+ description="Upload an image to perform Optical Character Recognition (OCR)."
35
+ )
36
 
37
+ if _name_ == "_main_":
38
+ iface.launch()