rockerritesh commited on
Commit
cd1242f
·
verified ·
1 Parent(s): 8a07592

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -3,21 +3,25 @@ import requests
3
  import json # Add this import to parse JSON responses
4
 
5
  def extract_text_from_image(uploaded_file):
6
- """Upload image to the text extraction API and return extracted text"""
7
- files = {'image': uploaded_file}
8
-
9
  try:
 
 
 
 
 
 
 
 
 
 
 
10
  response = requests.post('https://api-1-zvvu.onrender.com/upload', files=files)
11
-
12
  if response.status_code == 200:
13
- # Parse JSON response
14
  response_data = response.json()
15
- if "text" in response_data:
16
- # Load text as a Python object if it's JSON stringified
17
- extracted_text = json.loads(response_data["text"])
18
- return "\n".join(extracted_text) if isinstance(extracted_text, list) else extracted_text
19
- else:
20
- return "No text found in the response."
21
  else:
22
  return f"Error: {response.status_code} - {response.text}"
23
 
@@ -25,6 +29,7 @@ def extract_text_from_image(uploaded_file):
25
  return f"Request failed: {e}"
26
 
27
 
 
28
  def main():
29
  st.title('Image Text Extraction')
30
 
 
3
  import json # Add this import to parse JSON responses
4
 
5
  def extract_text_from_image(uploaded_file):
6
+ """Upload image to the text extraction API and return extracted text."""
 
 
7
  try:
8
+ # Reset file pointer to the beginning
9
+ uploaded_file.seek(0)
10
+
11
+ # Set the correct filename and MIME type
12
+ file_name = uploaded_file.name if uploaded_file.name else "uploaded_image.png"
13
+ mime_type = uploaded_file.type if uploaded_file.type else "image/png"
14
+
15
+ # Prepare the file tuple (filename, file object, MIME type)
16
+ files = {'image': (file_name, uploaded_file, mime_type)}
17
+
18
+ # Make the API call
19
  response = requests.post('https://api-1-zvvu.onrender.com/upload', files=files)
20
+
21
  if response.status_code == 200:
22
+ # Parse and return the response
23
  response_data = response.json()
24
+ return response_data.get("text", "No text extracted.")
 
 
 
 
 
25
  else:
26
  return f"Error: {response.status_code} - {response.text}"
27
 
 
29
  return f"Request failed: {e}"
30
 
31
 
32
+
33
  def main():
34
  st.title('Image Text Extraction')
35