Spaces:
Running
Running
remove ocr and auth message
Browse files
app.py
CHANGED
|
@@ -4,12 +4,8 @@ from http import HTTPStatus
|
|
| 4 |
from typing import Dict, List, Optional, Tuple
|
| 5 |
import base64
|
| 6 |
import mimetypes
|
| 7 |
-
import PyPDF2
|
| 8 |
-
import docx
|
| 9 |
-
import cv2
|
| 10 |
import numpy as np
|
| 11 |
from PIL import Image
|
| 12 |
-
import pytesseract
|
| 13 |
import requests
|
| 14 |
from urllib.parse import urlparse, urljoin
|
| 15 |
from bs4 import BeautifulSoup
|
|
@@ -5450,63 +5446,6 @@ def demo_card_click(e: gr.EventData):
|
|
| 5450 |
except (KeyError, IndexError, AttributeError) as e:
|
| 5451 |
# Return the first demo description as fallback
|
| 5452 |
return DEMO_LIST[0]['description']
|
| 5453 |
-
def extract_text_from_image(image_path):
|
| 5454 |
-
"""Extract text from image using OCR"""
|
| 5455 |
-
try:
|
| 5456 |
-
# Check if tesseract is available
|
| 5457 |
-
try:
|
| 5458 |
-
pytesseract.get_tesseract_version()
|
| 5459 |
-
except Exception:
|
| 5460 |
-
return "Error: Tesseract OCR is not installed. Please install Tesseract to extract text from images. See install_tesseract.md for instructions."
|
| 5461 |
-
|
| 5462 |
-
# Read image using OpenCV
|
| 5463 |
-
image = cv2.imread(image_path)
|
| 5464 |
-
if image is None:
|
| 5465 |
-
return "Error: Could not read image file"
|
| 5466 |
-
|
| 5467 |
-
# Convert to RGB (OpenCV uses BGR)
|
| 5468 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 5469 |
-
|
| 5470 |
-
# Preprocess image for better OCR results
|
| 5471 |
-
# Convert to grayscale
|
| 5472 |
-
gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY)
|
| 5473 |
-
|
| 5474 |
-
# Apply thresholding to get binary image
|
| 5475 |
-
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 5476 |
-
|
| 5477 |
-
# Extract text using pytesseract
|
| 5478 |
-
text = pytesseract.image_to_string(binary, config='--psm 6')
|
| 5479 |
-
|
| 5480 |
-
return text.strip() if text.strip() else "No text found in image"
|
| 5481 |
-
|
| 5482 |
-
except Exception as e:
|
| 5483 |
-
return f"Error extracting text from image: {e}"
|
| 5484 |
-
|
| 5485 |
-
def extract_text_from_file(file_path):
|
| 5486 |
-
if not file_path:
|
| 5487 |
-
return ""
|
| 5488 |
-
mime, _ = mimetypes.guess_type(file_path)
|
| 5489 |
-
ext = os.path.splitext(file_path)[1].lower()
|
| 5490 |
-
try:
|
| 5491 |
-
if ext == ".pdf":
|
| 5492 |
-
with open(file_path, "rb") as f:
|
| 5493 |
-
reader = PyPDF2.PdfReader(f)
|
| 5494 |
-
return "\n".join(page.extract_text() or "" for page in reader.pages)
|
| 5495 |
-
elif ext in [".txt", ".md"]:
|
| 5496 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 5497 |
-
return f.read()
|
| 5498 |
-
elif ext == ".csv":
|
| 5499 |
-
with open(file_path, "r", encoding="utf-8") as f:
|
| 5500 |
-
return f.read()
|
| 5501 |
-
elif ext == ".docx":
|
| 5502 |
-
doc = docx.Document(file_path)
|
| 5503 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
| 5504 |
-
elif ext.lower() in [".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".gif", ".webp"]:
|
| 5505 |
-
return extract_text_from_image(file_path)
|
| 5506 |
-
else:
|
| 5507 |
-
return ""
|
| 5508 |
-
except Exception as e:
|
| 5509 |
-
return f"Error extracting text: {e}"
|
| 5510 |
|
| 5511 |
def extract_website_content(url: str) -> str:
|
| 5512 |
"""Extract HTML code and content from a website URL"""
|
|
@@ -5866,12 +5805,7 @@ def update_ui_for_auth_status(profile: gr.OAuthProfile | None = None, token: gr.
|
|
| 5866 |
return {
|
| 5867 |
# Enable main input and button
|
| 5868 |
input: gr.update(interactive=True, placeholder="Describe your application..."),
|
| 5869 |
-
btn: gr.update(interactive=True, variant="primary")
|
| 5870 |
-
# Show authentication status
|
| 5871 |
-
auth_status: gr.update(
|
| 5872 |
-
value=f"β
{auth_message}",
|
| 5873 |
-
visible=True
|
| 5874 |
-
)
|
| 5875 |
}
|
| 5876 |
else:
|
| 5877 |
# User not authenticated - disable main components
|
|
@@ -5881,16 +5815,11 @@ def update_ui_for_auth_status(profile: gr.OAuthProfile | None = None, token: gr.
|
|
| 5881 |
interactive=False,
|
| 5882 |
placeholder="π Please log in with Hugging Face to use AnyCoder..."
|
| 5883 |
),
|
| 5884 |
-
btn: gr.update(interactive=False, variant="secondary")
|
| 5885 |
-
# Show authentication requirement
|
| 5886 |
-
auth_status: gr.update(
|
| 5887 |
-
value=f"π {auth_message}",
|
| 5888 |
-
visible=True
|
| 5889 |
-
)
|
| 5890 |
}
|
| 5891 |
|
| 5892 |
|
| 5893 |
-
def generation_code(query: str | None, vlm_image: Optional[gr.Image],
|
| 5894 |
# Check authentication first
|
| 5895 |
is_authenticated, auth_message = check_authentication(profile, token)
|
| 5896 |
if not is_authenticated:
|
|
@@ -6085,13 +6014,6 @@ Generate the exact search/replace blocks needed to make these changes."""
|
|
| 6085 |
|
| 6086 |
messages = history_to_messages(_history, system_prompt)
|
| 6087 |
|
| 6088 |
-
# Extract file text and append to query if file is present
|
| 6089 |
-
file_text = ""
|
| 6090 |
-
if file:
|
| 6091 |
-
file_text = extract_text_from_file(file)
|
| 6092 |
-
if file_text:
|
| 6093 |
-
file_text = file_text[:5000] # Limit to 5000 chars for prompt size
|
| 6094 |
-
query = f"{query}\n\n[Reference file content below]\n{file_text}"
|
| 6095 |
|
| 6096 |
# Extract website content and append to query if website URL is present
|
| 6097 |
website_text = ""
|
|
@@ -6815,7 +6737,7 @@ def generate_requirements_txt_with_llm(import_statements):
|
|
| 6815 |
Instructions:
|
| 6816 |
- Include the direct packages needed for the imports
|
| 6817 |
- Include commonly used companion packages and dependencies for better functionality
|
| 6818 |
-
- Use correct PyPI package names (e.g.,
|
| 6819 |
- IMPORTANT: For diffusers, ALWAYS use: git+https://github.com/huggingface/diffusers
|
| 6820 |
- IMPORTANT: For transformers, ALWAYS use: git+https://github.com/huggingface/transformers
|
| 6821 |
- IMPORTANT: If diffusers is installed, also include transformers and sentencepiece as they usually go together
|
|
@@ -6876,7 +6798,6 @@ Generate a comprehensive requirements.txt that ensures the application will work
|
|
| 6876 |
# Fallback: simple extraction with basic mapping
|
| 6877 |
dependencies = set()
|
| 6878 |
special_cases = {
|
| 6879 |
-
'cv2': 'opencv-python',
|
| 6880 |
'PIL': 'Pillow',
|
| 6881 |
'sklearn': 'scikit-learn',
|
| 6882 |
'skimage': 'scikit-image',
|
|
@@ -7734,12 +7655,6 @@ with gr.Blocks(
|
|
| 7734 |
with gr.Sidebar() as sidebar:
|
| 7735 |
login_button = gr.LoginButton()
|
| 7736 |
|
| 7737 |
-
# Authentication status display
|
| 7738 |
-
auth_status = gr.Markdown(
|
| 7739 |
-
value="π Please log in with your Hugging Face account to use AnyCoder.",
|
| 7740 |
-
visible=True,
|
| 7741 |
-
elem_classes=["auth-status"]
|
| 7742 |
-
)
|
| 7743 |
|
| 7744 |
|
| 7745 |
|
|
@@ -7805,11 +7720,6 @@ with gr.Blocks(
|
|
| 7805 |
lines=1,
|
| 7806 |
visible=True
|
| 7807 |
)
|
| 7808 |
-
file_input = gr.File(
|
| 7809 |
-
label="Reference file (OCR only)",
|
| 7810 |
-
file_types=[".pdf", ".txt", ".md", ".csv", ".docx", ".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".gif", ".webp"],
|
| 7811 |
-
visible=True
|
| 7812 |
-
)
|
| 7813 |
image_input = gr.Image(
|
| 7814 |
label="UI design image",
|
| 7815 |
visible=False
|
|
@@ -8396,7 +8306,7 @@ with gr.Blocks(
|
|
| 8396 |
show_progress="hidden",
|
| 8397 |
).then(
|
| 8398 |
generation_code,
|
| 8399 |
-
inputs=[input, image_input,
|
| 8400 |
outputs=[code_output, history, sandbox, history_output]
|
| 8401 |
).then(
|
| 8402 |
end_generation_ui,
|
|
@@ -8437,7 +8347,7 @@ with gr.Blocks(
|
|
| 8437 |
show_progress="hidden",
|
| 8438 |
).then(
|
| 8439 |
generation_code,
|
| 8440 |
-
inputs=[input, image_input,
|
| 8441 |
outputs=[code_output, history, sandbox, history_output]
|
| 8442 |
).then(
|
| 8443 |
end_generation_ui,
|
|
@@ -8471,7 +8381,7 @@ with gr.Blocks(
|
|
| 8471 |
language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
|
| 8472 |
# Update deploy button text when space name changes
|
| 8473 |
space_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])
|
| 8474 |
-
clear_btn.click(clear_history, outputs=[history, history_output,
|
| 8475 |
clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])
|
| 8476 |
# Reset space name and button text when clearing
|
| 8477 |
clear_btn.click(
|
|
@@ -9121,7 +9031,7 @@ with gr.Blocks(
|
|
| 9121 |
login_button.click(
|
| 9122 |
handle_auth_update,
|
| 9123 |
inputs=[],
|
| 9124 |
-
outputs=[input, btn
|
| 9125 |
queue=False
|
| 9126 |
)
|
| 9127 |
|
|
@@ -9129,7 +9039,7 @@ with gr.Blocks(
|
|
| 9129 |
demo.load(
|
| 9130 |
handle_auth_update,
|
| 9131 |
inputs=[],
|
| 9132 |
-
outputs=[input, btn
|
| 9133 |
queue=False
|
| 9134 |
)
|
| 9135 |
|
|
|
|
| 4 |
from typing import Dict, List, Optional, Tuple
|
| 5 |
import base64
|
| 6 |
import mimetypes
|
|
|
|
|
|
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
from PIL import Image
|
|
|
|
| 9 |
import requests
|
| 10 |
from urllib.parse import urlparse, urljoin
|
| 11 |
from bs4 import BeautifulSoup
|
|
|
|
| 5446 |
except (KeyError, IndexError, AttributeError) as e:
|
| 5447 |
# Return the first demo description as fallback
|
| 5448 |
return DEMO_LIST[0]['description']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5449 |
|
| 5450 |
def extract_website_content(url: str) -> str:
|
| 5451 |
"""Extract HTML code and content from a website URL"""
|
|
|
|
| 5805 |
return {
|
| 5806 |
# Enable main input and button
|
| 5807 |
input: gr.update(interactive=True, placeholder="Describe your application..."),
|
| 5808 |
+
btn: gr.update(interactive=True, variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5809 |
}
|
| 5810 |
else:
|
| 5811 |
# User not authenticated - disable main components
|
|
|
|
| 5815 |
interactive=False,
|
| 5816 |
placeholder="π Please log in with Hugging Face to use AnyCoder..."
|
| 5817 |
),
|
| 5818 |
+
btn: gr.update(interactive=False, variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5819 |
}
|
| 5820 |
|
| 5821 |
|
| 5822 |
+
def generation_code(query: str | None, vlm_image: Optional[gr.Image], website_url: str | None, _setting: Dict[str, str], _history: Optional[History], _current_model: Dict, language: str = "html", provider: str = "auto", profile: gr.OAuthProfile | None = None, token: gr.OAuthToken | None = None):
|
| 5823 |
# Check authentication first
|
| 5824 |
is_authenticated, auth_message = check_authentication(profile, token)
|
| 5825 |
if not is_authenticated:
|
|
|
|
| 6014 |
|
| 6015 |
messages = history_to_messages(_history, system_prompt)
|
| 6016 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6017 |
|
| 6018 |
# Extract website content and append to query if website URL is present
|
| 6019 |
website_text = ""
|
|
|
|
| 6737 |
Instructions:
|
| 6738 |
- Include the direct packages needed for the imports
|
| 6739 |
- Include commonly used companion packages and dependencies for better functionality
|
| 6740 |
+
- Use correct PyPI package names (e.g., PIL -> Pillow, sklearn -> scikit-learn)
|
| 6741 |
- IMPORTANT: For diffusers, ALWAYS use: git+https://github.com/huggingface/diffusers
|
| 6742 |
- IMPORTANT: For transformers, ALWAYS use: git+https://github.com/huggingface/transformers
|
| 6743 |
- IMPORTANT: If diffusers is installed, also include transformers and sentencepiece as they usually go together
|
|
|
|
| 6798 |
# Fallback: simple extraction with basic mapping
|
| 6799 |
dependencies = set()
|
| 6800 |
special_cases = {
|
|
|
|
| 6801 |
'PIL': 'Pillow',
|
| 6802 |
'sklearn': 'scikit-learn',
|
| 6803 |
'skimage': 'scikit-image',
|
|
|
|
| 7655 |
with gr.Sidebar() as sidebar:
|
| 7656 |
login_button = gr.LoginButton()
|
| 7657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7658 |
|
| 7659 |
|
| 7660 |
|
|
|
|
| 7720 |
lines=1,
|
| 7721 |
visible=True
|
| 7722 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7723 |
image_input = gr.Image(
|
| 7724 |
label="UI design image",
|
| 7725 |
visible=False
|
|
|
|
| 8306 |
show_progress="hidden",
|
| 8307 |
).then(
|
| 8308 |
generation_code,
|
| 8309 |
+
inputs=[input, image_input, website_url_input, setting, history, current_model, language_dropdown, provider_state],
|
| 8310 |
outputs=[code_output, history, sandbox, history_output]
|
| 8311 |
).then(
|
| 8312 |
end_generation_ui,
|
|
|
|
| 8347 |
show_progress="hidden",
|
| 8348 |
).then(
|
| 8349 |
generation_code,
|
| 8350 |
+
inputs=[input, image_input, website_url_input, setting, history, current_model, language_dropdown, provider_state],
|
| 8351 |
outputs=[code_output, history, sandbox, history_output]
|
| 8352 |
).then(
|
| 8353 |
end_generation_ui,
|
|
|
|
| 8381 |
language_dropdown.change(preview_logic, inputs=[code_output, language_dropdown, tjs_html_code, tjs_js_code, tjs_css_code], outputs=sandbox)
|
| 8382 |
# Update deploy button text when space name changes
|
| 8383 |
space_name_input.change(update_deploy_button_text, inputs=[space_name_input], outputs=[deploy_btn])
|
| 8384 |
+
clear_btn.click(clear_history, outputs=[history, history_output, website_url_input])
|
| 8385 |
clear_btn.click(hide_deploy_components, None, [space_name_input, sdk_dropdown, deploy_btn])
|
| 8386 |
# Reset space name and button text when clearing
|
| 8387 |
clear_btn.click(
|
|
|
|
| 9031 |
login_button.click(
|
| 9032 |
handle_auth_update,
|
| 9033 |
inputs=[],
|
| 9034 |
+
outputs=[input, btn],
|
| 9035 |
queue=False
|
| 9036 |
)
|
| 9037 |
|
|
|
|
| 9039 |
demo.load(
|
| 9040 |
handle_auth_update,
|
| 9041 |
inputs=[],
|
| 9042 |
+
outputs=[input, btn],
|
| 9043 |
queue=False
|
| 9044 |
)
|
| 9045 |
|