shashankkandimalla commited on
Commit
8860032
1 Parent(s): 77087fc

update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -40
app.py CHANGED
@@ -6,22 +6,23 @@ import base64
6
  import os
7
  from dotenv import load_dotenv
8
  from openai import OpenAI
 
 
9
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
  # Function to upload image to imgbb
14
- def upload_image_to_imgbb(image_path):
15
  try:
16
- with open(image_path, "rb") as file:
17
- url = "https://api.imgbb.com/1/upload"
18
- payload = {
19
- "key": os.getenv("IMGBB_API_KEY"),
20
- "image": base64.b64encode(file.read()).decode()
21
- }
22
- res = requests.post(url, data=payload)
23
- res.raise_for_status() # Raises an HTTPError for bad responses
24
- return res.json()['data']['url']
25
  except Exception as e:
26
  return f"Error uploading image: {str(e)}"
27
 
@@ -54,7 +55,7 @@ def process_ocr_with_gpt(ocr_results):
54
 
55
  {ocr_results}
56
 
57
- Please process this information and provide a clean, well-formatted output. If there are any obvious errors in the OCR, please correct them. Format the output in a clear and readable manner.
58
  """
59
 
60
  response = openai_client.chat.completions.create(
@@ -70,42 +71,64 @@ def process_ocr_with_gpt(ocr_results):
70
  return f"Error in GPT processing: {str(e)}"
71
 
72
  # Gradio interface function
73
- def process_image(image):
74
- try:
75
- # Save the image temporarily and get its path
76
- image_path = "temp_image.png"
77
- image.save(image_path)
78
-
79
- # Upload image to imgbb and get URL
80
- image_url = upload_image_to_imgbb(image_path)
81
- if image_url.startswith("Error"):
82
- return image_url, "Failed to process due to image upload error"
83
-
84
- # Get OCR results
85
- ocr_results = get_ocr_results(image_url)
86
- if ocr_results.startswith("Error"):
87
- return ocr_results, "Failed to process due to OCR error"
88
-
89
- # Process with GPT
90
- processed_results = process_ocr_with_gpt(ocr_results)
91
-
92
- # Clean up temporary file
93
- os.remove(image_path)
94
-
95
- return ocr_results, processed_results
96
- except Exception as e:
97
- return f"Error in image processing: {str(e)}", "Failed to process due to an error"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  # Create Gradio interface
100
  iface = gr.Interface(
101
- fn=process_image,
102
- inputs=gr.Image(type="pil"),
103
  outputs=[
104
  gr.Textbox(label="OCR Results"),
105
- gr.Textbox(label="Processed Results")
 
106
  ],
107
  title="OCR and Text Processing App",
108
- description="Upload an image to extract text and process it."
109
  )
110
 
111
  # Launch the app
 
6
  import os
7
  from dotenv import load_dotenv
8
  from openai import OpenAI
9
+ from PIL import Image
10
+ import io
11
 
12
  # Load environment variables
13
  load_dotenv()
14
 
15
  # Function to upload image to imgbb
16
+ def upload_image_to_imgbb(image_bytes):
17
  try:
18
+ url = "https://api.imgbb.com/1/upload"
19
+ payload = {
20
+ "key": os.getenv("IMGBB_API_KEY"),
21
+ "image": base64.b64encode(image_bytes).decode()
22
+ }
23
+ res = requests.post(url, data=payload)
24
+ res.raise_for_status() # Raises an HTTPError for bad responses
25
+ return res.json()['data']['url']
 
26
  except Exception as e:
27
  return f"Error uploading image: {str(e)}"
28
 
 
55
 
56
  {ocr_results}
57
 
58
+ Please process this information and provide a clean, well-formatted output. Arrange all elements in order and omit any elements not present in the file.
59
  """
60
 
61
  response = openai_client.chat.completions.create(
 
71
  return f"Error in GPT processing: {str(e)}"
72
 
73
  # Gradio interface function
74
+ def process_images(files):
75
+ ocr_results_list = []
76
+ processed_results_list = []
77
+ download_links = []
78
+
79
+ for idx, file in enumerate(files):
80
+ try:
81
+ # Read the uploaded binary file
82
+ image = Image.open(io.BytesIO(file))
83
+ image_path = f"temp_image_{idx}.png"
84
+ image.save(image_path)
85
+
86
+ # Upload image to imgbb and get URL
87
+ image_url = upload_image_to_imgbb(file)
88
+ if image_url.startswith("Error"):
89
+ ocr_results_list.append(image_url)
90
+ processed_results_list.append("Failed to process due to image upload error")
91
+ continue
92
+
93
+ # Get OCR results
94
+ ocr_results = get_ocr_results(image_url)
95
+ if ocr_results.startswith("Error"):
96
+ ocr_results_list.append(ocr_results)
97
+ processed_results_list.append("Failed to process due to OCR error")
98
+ continue
99
+
100
+ # Process with GPT
101
+ processed_results = process_ocr_with_gpt(ocr_results)
102
+
103
+ # Save processed results to a file for download
104
+ result_file_path = f"processed_result_{idx}.txt"
105
+ with open(result_file_path, 'w') as result_file:
106
+ result_file.write(processed_results)
107
+
108
+ download_links.append(result_file_path)
109
+
110
+ # Clean up temporary file
111
+ os.remove(image_path)
112
+
113
+ ocr_results_list.append(ocr_results)
114
+ processed_results_list.append(processed_results)
115
+ except Exception as e:
116
+ ocr_results_list.append(f"Error in image processing: {str(e)}")
117
+ processed_results_list.append("Failed to process due to an error")
118
+
119
+ return ocr_results_list, processed_results_list, download_links
120
 
121
  # Create Gradio interface
122
  iface = gr.Interface(
123
+ fn=process_images,
124
+ inputs=gr.Files(label="Upload Images", file_count="multiple", type="binary"),
125
  outputs=[
126
  gr.Textbox(label="OCR Results"),
127
+ gr.Textbox(label="Processed Results"),
128
+ gr.File(label="Download Processed Results")
129
  ],
130
  title="OCR and Text Processing App",
131
+ description="Upload images to extract text and process it. Download the processed results."
132
  )
133
 
134
  # Launch the app