update image text extract
Browse filesFiles changed (3)
- README.md+18
- app.py+47-3
- requirements.txt+4-1
20 unmodified lines2122232425262748 unmodified lines767778798081828384858687888990919293949596979820 unmodified lines
- **Multi-Model Support**: Choose from various AI models including DeepSeek, ERNIE-4.5-VL, MiniMax, and Qwen- **Image-to-Code**: Upload UI design images and get corresponding HTML/CSS code (ERNIE-4.5-VL model)- **Image Text Extraction**: Upload images and extract text using OCR for processing- **Live Preview**: See your generated code in action with the built-in sandbox- **Web Search Integration**: Enable real-time web search to get the latest information and best practices- **Chat History**: Keep track of your conversations and generated code48 unmodified lines
When enabled, the AI will search the web for the latest information, best practices, and technologies related to your request.
## Image Text Extraction
The application supports extracting text from images using OCR (Optical Character Recognition). This feature allows you to:
1. Upload image files (JPG, PNG, BMP, TIFF, GIF, WebP) through the file input2. Automatically extract text from the images using Tesseract OCR3. Include the extracted text in your prompts for code generation
### Setting up OCR
To use the image text extraction feature, you need to install Tesseract OCR on your system. See `install_tesseract.md` for detailed installation instructions.
**Example usage:**- Upload an image containing text (like a screenshot, document, or handwritten notes)- The application will extract the text and include it in your prompt- You can then ask the AI to process, summarize, or work with the extracted text
## Available Models
- **DeepSeek V3**: Advanced code generation model5 unmodified lines6789101112131415104 unmodified lines120121122123124125126127128129250 unmodified lines38038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641713 unmodified lines43143243343443543643743874 unmodified lines5135145154745165175185194 unmodified lines52452552648548652752852953053113 unmodified lines5455465475485495505515525 unmodified linesimport mimetypesimport PyPDF2import docximport cv2import numpy as npfrom PIL import Imageimport pytesseract
import gradio as grfrom huggingface_hub import InferenceClient104 unmodified lines { "title": "UI from Image", "description": "Upload an image of a UI design and I'll generate the HTML/CSS code for it" }, { "title": "Extract Text from Image", "description": "Upload an image containing text and I'll extract and process the text content" }]
250 unmodified lines # Return the first demo description as fallback return DEMO_LIST[0]['description']
def extract_text_from_image(image_path): """Extract text from image using OCR""" try: # Check if tesseract is available try: pytesseract.get_tesseract_version() except Exception: return "Error: Tesseract OCR is not installed. Please install Tesseract to extract text from images. See install_tesseract.md for instructions." # Read image using OpenCV image = cv2.imread(image_path) if image is None: return "Error: Could not read image file" # Convert to RGB (OpenCV uses BGR) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Preprocess image for better OCR results # Convert to grayscale gray = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2GRAY) # Apply thresholding to get binary image _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # Extract text using pytesseract text = pytesseract.image_to_string(binary, config='--psm 6') return text.strip() if text.strip() else "No text found in image" except Exception as e: return f"Error extracting text from image: {e}"
def extract_text_from_file(file_path): if not file_path: return ""13 unmodified lines elif ext == ".docx": doc = docx.Document(file_path) return "\n".join([para.text for para in doc.paragraphs]) elif ext.lower() in [".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".gif", ".webp"]: return extract_text_from_image(file_path) else: return "" except Exception as e:74 unmodified lines gr.Markdown("# AnyCoder\nAI-Powered Code Generator") gr.Markdown("""Describe your app or UI in plain English. Optionally upload a UI image (for ERNIE model). Click Generate to get code and preview.""") gr.Markdown("**Tip:** For best search results about people or entities, include details like profession, company, or location. Example: 'John Smith software engineer at Google.'") gr.Markdown("**Tip:** You can attach a file (PDF, TXT, DOCX, CSV, MD) to use as reference for your prompt, e.g. 'Summarize this PDF.'") gr.Markdown("**Tip:** You can attach a file (PDF, TXT, DOCX, CSV, MD, Images) to use as reference for your prompt, e.g. 'Summarize this PDF' or 'Extract text from this image'.") input = gr.Textbox( label="Describe your application", placeholder="e.g., Create a todo app with add, delete, and mark as complete functionality",4 unmodified lines visible=False ) file_input = gr.File( label="Attach a file (PDF, TXT, DOCX, CSV, MD)", file_types=[".pdf", ".txt", ".md", ".csv", ".docx"], label="Attach a file (PDF, TXT, DOCX, CSV, MD, Images)", file_types=[".pdf", ".txt", ".md", ".csv", ".docx", ".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".tif", ".gif", ".webp"], visible=True ) with gr.Row():13 unmodified lines else: gr.Markdown("✅ **Web Search Available**: Toggle above to enable real-time search") gr.Markdown("📷 **Image Text Extraction**: Upload images to extract text using OCR (requires Tesseract installation)") gr.Markdown("### Quick Examples") for i, demo_item in enumerate(DEMO_LIST[:5]): demo_card = gr.Button(1 unmodified line234556781 unmodified linegradio[oauth]tavily-pythonPyPDF2python-docxpython-docxpytesseractPillowopencv-pythonREADME.md+18
app.py+47-3
requirements.txt+4-1