Hammedalmodel commited on
Commit
2a6d894
·
verified ·
1 Parent(s): 234ce39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -42
app.py CHANGED
@@ -13,55 +13,47 @@ model = MllamaForConditionalGeneration.from_pretrained(
13
  processor = AutoProcessor.from_pretrained(ckpt)
14
 
15
  @spaces.GPU
16
- def extract_text(image_path):
17
- try:
18
- # Handle different types of image inputs
19
- if isinstance(image_path, dict) and "path" in image_path:
20
- image_path = image_path["path"]
21
-
22
- # Convert image to RGB
23
- image = Image.open(image_path).convert("RGB")
24
-
25
- # Create message structure
26
- messages = [
27
- {
28
- "role": "user",
29
- "content": [
30
- {"type": "text", "text": "Extract handwritten text from the image and output only the extracted text without any additional description or commentary in output"},
31
- {"type": "image"}
32
- ]
33
- }
34
- ]
35
-
36
- # Process input
37
- texts = processor.apply_chat_template(messages, add_generation_prompt=True)
38
- inputs = processor(text=texts, images=[image], return_tensors="pt").to("cuda")
39
-
40
- # Generate output
41
- outputs = model.generate(**inputs, max_new_tokens=250)
42
- result = processor.decode(outputs[0], skip_special_tokens=True)
43
-
44
- # Clean up the output
45
- if "assistant" in result.lower():
46
- result = result[result.lower().find("assistant") + len("assistant"):].strip()
47
-
48
- result = result.replace("user", "").replace("Extract handwritten text from the image and output only the extracted text without any additional description or commentary in output", "").strip()
49
-
50
- return f"\n{result}\n"
51
-
52
- except Exception as e:
53
- return f"Error processing image: {str(e)}"
54
 
55
  # Create Gradio interface
56
  demo = gr.Interface(
57
  fn=extract_text,
58
  inputs=gr.Image(type="filepath", label="Upload Image"),
59
- outputs=gr.Textbox(label="Extracted Text", lines=10), # Increased lines for better visibility
60
  title="Handwritten Text Extractor",
61
  description="Upload an image containing handwritten text to extract its content.",
62
- examples=[], # Add example images here if needed
63
  )
64
 
65
- # Launch the app with queue enabled
66
- demo.queue() # Enable queuing
67
  demo.launch(debug=True)
 
13
  processor = AutoProcessor.from_pretrained(ckpt)
14
 
15
  @spaces.GPU
16
+ def extract_text(image):
17
+ # Convert image to RGB
18
+ image = Image.open(image).convert("RGB")
19
+
20
+ # Create message structure
21
+ messages = [
22
+ {
23
+ "role": "user",
24
+ "content": [
25
+ {"type": "text", "text": "Extract handwritten text from the image and output only the extracted text without any additional description or commentary in output"},
26
+ {"type": "image"}
27
+ ]
28
+ }
29
+ ]
30
+
31
+ # Process input
32
+ texts = processor.apply_chat_template(messages, add_generation_prompt=True)
33
+ inputs = processor(text=texts, images=[image], return_tensors="pt").to("cuda")
34
+
35
+
36
+ # Generate output
37
+ outputs = model.generate(**inputs, max_new_tokens=250)
38
+ result = processor.decode(outputs[0], skip_special_tokens=True)
39
+
40
+ # Clean up the output to remove the prompt and assistant text
41
+ if "assistant" in result.lower():
42
+ result = result[result.lower().find("assistant") + len("assistant"):].strip()
43
+
44
+ # Remove any remaining conversation markers
45
+ result = result.replace("user", "").replace("Extract handwritten text from the image and output only the extracted text without any additional description or commentary in output", "").strip()
46
+
47
+ return result
 
 
 
 
 
 
48
 
49
  # Create Gradio interface
50
  demo = gr.Interface(
51
  fn=extract_text,
52
  inputs=gr.Image(type="filepath", label="Upload Image"),
53
+ outputs=gr.Textbox(label="Extracted Text"),
54
  title="Handwritten Text Extractor",
55
  description="Upload an image containing handwritten text to extract its content.",
 
56
  )
57
 
58
+ # Launch the app
 
59
  demo.launch(debug=True)