Kabilash10 commited on
Commit
ca56ed5
·
verified ·
1 Parent(s): 339e0fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -1,36 +1,36 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import requests
4
- import io
5
-
6
- # Function to perform OCR using Qwen model
7
- def perform_ocr(image):
8
- # Convert the image to bytes
9
- buffered = io.BytesIO()
10
- image.save(buffered, format="JPEG")
11
- image_bytes = buffered.getvalue()
12
-
13
- # API URL for Qwen OCR model
14
- api_url = "https://api-inference.huggingface.co/models/Qwen/Qwen-VL"
15
- headers = {"Authorization": f"Bearer {HF}"}
16
 
17
- # Make a request to the model
18
- response = requests.post(api_url, headers=headers, files={"file": image_bytes})
 
 
 
 
19
 
20
- # Check if the request was successful
21
- if response.status_code == 200:
22
- result = response.json()
23
- return result['text'] # Adjust according to the API response format
24
- else:
25
- return "Error: " + response.text
26
 
27
  # Gradio interface
28
  with gr.Blocks() as demo:
29
- gr.Markdown("## OCR with Qwen Model")
30
- image_input = gr.Image(type="pil", label="Upload Image")
31
- ocr_button = gr.Button("Perform OCR")
32
- text_output = gr.Textbox(label="Extracted Text", lines=10)
 
33
 
34
- ocr_button.click(perform_ocr, inputs=image_input, outputs=text_output)
 
 
35
 
 
36
  demo.launch()
 
1
  import gradio as gr
 
2
  import requests
3
+ import os
4
+
5
+ # Function to query the Qwen model for OCR
6
+ def query_ocr(image):
7
+ # Get the Hugging Face access token from secrets
8
+ hf_token = os.getenv("secret")
9
+ headers = {"Authorization": f"Bearer {hf_token}"}
10
+
11
+ # API endpoint for the model
12
+ API_URL = "https://api-inference.huggingface.co/models/Qwen/Qwen-VL"
 
 
13
 
14
+ # Prepare the request
15
+ files = {"file": image}
16
+ response = requests.post(API_URL, headers=headers, files=files)
17
+
18
+ # Raise an error if the request fails
19
+ response.raise_for_status()
20
 
21
+ return response.json() # Assuming the model returns a JSON with the extracted text
 
 
 
 
 
22
 
23
  # Gradio interface
24
  with gr.Blocks() as demo:
25
+ gr.Markdown("# OCR Application using Qwen Model")
26
+ gr.Markdown("Upload an image to extract text using OCR.")
27
+
28
+ image_input = gr.Image(type="file", label="Upload Image")
29
+ output_text = gr.Textbox(label="Extracted Text", lines=10)
30
 
31
+ submit_btn = gr.Button("Extract Text")
32
+
33
+ submit_btn.click(fn=query_ocr, inputs=image_input, outputs=output_text)
34
 
35
+ # Launch the app
36
  demo.launch()