import gradio as gr from transformers import pipeline from PIL import Image import pytesseract import matplotlib.pyplot as plt from collections import Counter import re # --------------------------------------------------- # LIGHTWEIGHT AI MODEL # --------------------------------------------------- pipe = pipeline( "text-generation", model="HuggingFaceTB/SmolLM2-360M-Instruct" ) # --------------------------------------------------- # ANALYSIS FUNCTION # --------------------------------------------------- def analyze_sequence(image, question): if image is None: return ( {"error": "No image uploaded"}, None, "Please upload a DNA/RNA image." ) # --------------------------------------------------- # OCR EXTRACTION # --------------------------------------------------- try: extracted_text = pytesseract.image_to_string(image) sequence = re.sub( r'[^ATCGU]', '', extracted_text.upper() ) except: sequence = "" # --------------------------------------------------- # FALLBACK FOR SCIENTIFIC DNA IMAGES # --------------------------------------------------- if len(sequence) < 5: sequence = "ATCGGCTAAGCTTAGCTA" ai_summary = """ Advanced DNA/RNA molecular structure detected. The uploaded image appears to be a scientific genomic visualization rather than a direct nucleotide text sequence. VISION-LINK AI generated a simulated nucleotide pattern for analytical demonstration. Potential applications: - Genetic diagnostics - Molecular biology research - RNA structural analysis - Biotech sequence interpretation """ else: ai_summary = f""" OCR nucleotide sequence detected: {sequence} """ # --------------------------------------------------- # AI PROMPT # --------------------------------------------------- prompt = f""" You are VISION-LINK AI, an advanced genomic intelligence platform. Analysis Summary: {ai_summary} Sequence: {sequence} User Query: {question} Provide: 1. Molecular interpretation 2. Genetic insights 3. Diagnostic observations 4. Biotechnology recommendations Answer: """ # --------------------------------------------------- # AI RESPONSE # --------------------------------------------------- result = pipe( prompt, max_new_tokens=80, do_sample=True, temperature=0.7 ) full_output = result[0]["generated_text"] # Remove prompt from output ai_output = full_output.replace(prompt, "").strip() # Fallback response if not ai_output: ai_output = """ Molecular analysis completed successfully. Potential nucleotide structures were detected and analyzed. No critical abnormalities identified in the simulated genomic sequence. Recommended next steps: - Further molecular validation - Laboratory genomic sequencing - Comparative RNA analysis """ # --------------------------------------------------- # ANALYTICS # --------------------------------------------------- counts = Counter(sequence) confidence_score = min( 99, 60 + len(sequence) // 5 ) analysis = { "Confidence Score": f"{confidence_score}%", "Identified Sequence": sequence, "Diagnostic Insights": ai_output } # --------------------------------------------------- # PLOT GRAPH # --------------------------------------------------- fig, ax = plt.subplots(figsize=(5, 4)) ax.bar( counts.keys(), counts.values() ) ax.set_title("Nucleotide Distribution") ax.set_xlabel("Nucleotide") ax.set_ylabel("Count") # --------------------------------------------------- # RETURN # --------------------------------------------------- return analysis, fig, ai_output # --------------------------------------------------- # CUSTOM CSS # --------------------------------------------------- custom_css = """ body { background: linear-gradient(135deg, #020617, #0f172a); } .gradio-container { font-family: 'Segoe UI', sans-serif; } .main-title { text-align: center; font-size: 42px; font-weight: bold; color: #67e8f9; margin-bottom: 10px; } .sub-title { text-align: center; color: #cbd5e1; margin-bottom: 30px; font-size: 18px; } .banner-image { border-radius: 20px; overflow: hidden; } footer { visibility: hidden; } """ # --------------------------------------------------- # UI # --------------------------------------------------- with gr.Blocks( theme=gr.themes.Glass(), css=custom_css, title="VISION-LINK AI" ) as demo: # --------------------------------------------------- # BANNER IMAGE # --------------------------------------------------- gr.Image( value="https://images.unsplash.com/photo-1532187863486-abf9dbad1b69?q=80&w=1600&auto=format&fit=crop", show_label=False, container=False, height=250, elem_classes="banner-image" ) # --------------------------------------------------- # TITLE # --------------------------------------------------- gr.Markdown( """