Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,39 +70,61 @@ def decimal_approximation_tool(number: float, decimals: int = 1) -> float:
|
|
| 70 |
|
| 71 |
def get_files_task_id_tool(task_id: str) -> str:
|
| 72 |
"""
|
| 73 |
-
Downloads the file associated with the given task_id
|
| 74 |
|
| 75 |
Args:
|
| 76 |
task_id (str): The ID of the task to fetch the file for.
|
| 77 |
|
| 78 |
Returns:
|
| 79 |
-
str: The file content as a string (e.g., text or image description).
|
| 80 |
|
| 81 |
Example:
|
| 82 |
>>> get_files_task_id_tool("task_1")
|
| 83 |
'Nutrition facts: Calories 390, Butterfat 11%'
|
| 84 |
"""
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
def image_processing_tool(file_content: str) -> str:
|
| 91 |
"""
|
| 92 |
-
Processes an image file (or its description) and extracts
|
| 93 |
|
| 94 |
Args:
|
| 95 |
file_content (str): The file content or description (e.g., from get_files_task_id_tool).
|
| 96 |
|
| 97 |
Returns:
|
| 98 |
-
str: Extracted text
|
| 99 |
|
| 100 |
Example:
|
| 101 |
>>> image_processing_tool("Nutrition facts: Calories 390, Butterfat 11%")
|
| 102 |
'Calories: 390, Butterfat: 11%'
|
| 103 |
"""
|
| 104 |
-
#
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 108 |
"""
|
|
|
|
| 70 |
|
| 71 |
def get_files_task_id_tool(task_id: str) -> str:
|
| 72 |
"""
|
| 73 |
+
Downloads the file associated with the given task_id by making an API call.
|
| 74 |
|
| 75 |
Args:
|
| 76 |
task_id (str): The ID of the task to fetch the file for.
|
| 77 |
|
| 78 |
Returns:
|
| 79 |
+
str: The file content as a string (e.g., text or image description), or an error message.
|
| 80 |
|
| 81 |
Example:
|
| 82 |
>>> get_files_task_id_tool("task_1")
|
| 83 |
'Nutrition facts: Calories 390, Butterfat 11%'
|
| 84 |
"""
|
| 85 |
+
try:
|
| 86 |
+
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}")
|
| 87 |
+
if response.status_code == 200:
|
| 88 |
+
return response.text # Assuming the API returns the file content as a string
|
| 89 |
+
else:
|
| 90 |
+
return f"Error fetching file for task_id {task_id}: Status {response.status_code}"
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Error fetching file for task_id {task_id}: {str(e)}"
|
| 93 |
|
| 94 |
def image_processing_tool(file_content: str) -> str:
|
| 95 |
"""
|
| 96 |
+
Processes an image file (or its description) and extracts text using OCR.
|
| 97 |
|
| 98 |
Args:
|
| 99 |
file_content (str): The file content or description (e.g., from get_files_task_id_tool).
|
| 100 |
|
| 101 |
Returns:
|
| 102 |
+
str: Extracted text from the image or description.
|
| 103 |
|
| 104 |
Example:
|
| 105 |
>>> image_processing_tool("Nutrition facts: Calories 390, Butterfat 11%")
|
| 106 |
'Calories: 390, Butterfat: 11%'
|
| 107 |
"""
|
| 108 |
+
# Since API returns a string, we simulate OCR on the description for now
|
| 109 |
+
# Placeholder for real OCR: If file_content were an image path or bytes, we'd use pytesseract
|
| 110 |
+
try:
|
| 111 |
+
# Simulated OCR processing on the string (as a placeholder)
|
| 112 |
+
# In a real scenario, file_content would be an image file path or bytes
|
| 113 |
+
extracted_text = file_content.replace("Nutrition facts: ", "") # Mock extraction
|
| 114 |
+
return f"Extracted: {extracted_text}"
|
| 115 |
+
except Exception as e:
|
| 116 |
+
return f"OCR error: {str(e)}"
|
| 117 |
+
# Uncomment below for actual OCR when file_content is an image path
|
| 118 |
+
"""
|
| 119 |
+
try:
|
| 120 |
+
from PIL import Image
|
| 121 |
+
import pytesseract
|
| 122 |
+
image = Image.open(file_content) # Assuming file_content is a path to an image
|
| 123 |
+
extracted_text = pytesseract.image_to_string(image)
|
| 124 |
+
return f"Extracted: {extracted_text.strip()}"
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return f"OCR error: {str(e)}"
|
| 127 |
+
"""
|
| 128 |
|
| 129 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 130 |
"""
|