raviix46 commited on
Commit
77499e7
·
verified ·
1 Parent(s): 9d8c4f9

Rename components/llm_ocr_api.py to components/llm_ocr_gcv.py

Browse files
components/llm_ocr_api.py DELETED
@@ -1,15 +0,0 @@
1
- from gradio_client import Client, handle_file
2
-
3
- def extract_text_from_space(image_path):
4
- try:
5
- # Create client for Hugging Face Space
6
- client = Client("Hammedalmodel/handwritten_to_text")
7
-
8
- # Send image file path to the Space
9
- result = client.predict(
10
- image=handle_file(image_path), # handle_file needs a path
11
- api_name="/predict"
12
- )
13
- return result # Expected to be a single string output
14
- except Exception as e:
15
- return f"❌ Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
components/llm_ocr_gcv.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
+ from google.cloud import vision
4
+ from PIL import Image
5
+
6
+ def get_vision_client():
7
+ api_key_path = os.getenv("GCV_API_KEY") # ✅ Path to JSON from Hugging Face secret
8
+ if not api_key_path:
9
+ raise ValueError("❌ GCV_API_KEY not set in environment variables.")
10
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = api_key_path
11
+ return vision.ImageAnnotatorClient()
12
+
13
+ def extract_text_gcv(pil_image):
14
+ try:
15
+ client = get_vision_client()
16
+
17
+ buffer = io.BytesIO()
18
+ pil_image.save(buffer, format="PNG")
19
+ image = vision.Image(content=buffer.getvalue())
20
+
21
+ response = client.text_detection(image=image)
22
+ if response.error.message:
23
+ return f"❌ Google Vision Error: {response.error.message}"
24
+
25
+ annotations = response.text_annotations
26
+ if annotations:
27
+ return annotations[0].description.strip()
28
+ else:
29
+ return "❌ No text found in the image."
30
+
31
+ except Exception as e:
32
+ return f"❌ Exception: {e}"