import gradio as gr import pytesseract from PIL import Image, ImageEnhance, ImageFilter import requests import os import re def validate_subscription(image_url, channel_handle): try: # Download the image response = requests.get(image_url, stream=True) if response.status_code != 200: return "Error: Unable to download the image." temp_image_path = "temp_screenshot.jpg" with open(temp_image_path, "wb") as file: for chunk in response.iter_content(1024): file.write(chunk) # Open the image image = Image.open(temp_image_path) # Convert image to grayscale image = image.convert("L") # Apply sharpening and contrast enhancement image = image.filter(ImageFilter.SHARPEN) image = ImageEnhance.Contrast(image).enhance(2) # Perform OCR with optimized settings extracted_text = pytesseract.image_to_string(image, config="--psm 6").strip().lower() # Delete the temporary image os.remove(temp_image_path) # Debugging: Print extracted text to check accuracy print("Extracted Text:", extracted_text) # Normalize channel handle channel_handle = channel_handle.strip() # Check for subscription status if "subscribed" in extracted_text.lower(): # Use regex to match the exact channel handle (word boundaries ensure full match) pattern = rf"\b{re.escape(channel_handle)}\b" if re.search(pattern, extracted_text): return f"Subscription to {channel_handle} is valid." # If "Subscribed" is missing or the handle does not match exactly return f"Subscription to {channel_handle} is not valid." except Exception as e: return f"Error: {str(e)}" # Create the Gradio interface iface = gr.Interface( fn=validate_subscription, inputs=[ gr.Textbox(label="Image URL"), # Accepts Image URL gr.Textbox(label="YouTube Channel Handle") ], outputs="text", title="YouTube Subscription Validator", description="Enter the image URL of your subscription status and the YouTube channel handle to validate your subscription." ) # Launch the interface iface.launch(show_error=True)