Spaces:
Runtime error
Runtime error
""" | |
BILLION DOLLAR EDUCATION AI - MULTIMODAL DATASET SUPREMACY | |
100% Free β’ Groq-Only β’ Dataset-Powered β’ Images + PDFs + Documents | |
The Ultimate Educational AI with File Processing Capabilities | |
""" | |
import gradio as gr | |
import requests | |
import json | |
import random | |
import threading | |
import time | |
import base64 | |
import io | |
import os | |
from typing import Dict, List, Optional, Union | |
import asyncio | |
import aiohttp | |
from PIL import Image | |
import PyPDF2 | |
import docx | |
import pandas as pd | |
# Safe dataset import | |
try: | |
from datasets import load_dataset | |
DATASETS_AVAILABLE = True | |
except ImportError: | |
DATASETS_AVAILABLE = False | |
def load_dataset(*args, **kwargs): | |
return [] | |
class MultimodalProcessor: | |
"""Handles images, PDFs, documents, and other file types""" | |
def __init__(self): | |
self.supported_formats = { | |
'images': ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.webp'], | |
'documents': ['.pdf', '.docx', '.doc', '.txt'], | |
'data': ['.csv', '.xlsx', '.xls'], | |
'code': ['.py', '.js', '.html', '.css', '.java', '.cpp', '.c'] | |
} | |
def process_file(self, file_path: str) -> Dict[str, str]: | |
"""Process uploaded file and extract content/description""" | |
if not file_path or not os.path.exists(file_path): | |
return {"type": "error", "content": "File not found"} | |
file_ext = os.path.splitext(file_path)[1].lower() | |
try: | |
if file_ext in self.supported_formats['images']: | |
return self.process_image(file_path) | |
elif file_ext in self.supported_formats['documents']: | |
return self.process_document(file_path) | |
elif file_ext in self.supported_formats['data']: | |
return self.process_data_file(file_path) | |
elif file_ext in self.supported_formats['code']: | |
return self.process_code_file(file_path) | |
else: | |
return {"type": "unknown", "content": f"Unsupported file type: {file_ext}"} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing file: {str(e)}"} | |
def process_image(self, image_path: str) -> Dict[str, str]: | |
"""Process image files - describe content for educational context""" | |
try: | |
with Image.open(image_path) as img: | |
# Convert to base64 for potential API calls | |
buffer = io.BytesIO() | |
img.save(buffer, format='PNG') | |
img_base64 = base64.b64encode(buffer.getvalue()).decode() | |
# Basic image analysis | |
width, height = img.size | |
mode = img.mode | |
format_type = img.format | |
description = f"""IMAGE ANALYSIS: | |
- Dimensions: {width}x{height} pixels | |
- Format: {format_type} | |
- Color Mode: {mode} | |
- File Size: {os.path.getsize(image_path)} bytes | |
EDUCATIONAL CONTEXT: | |
This appears to be an image that may contain: | |
- Mathematical diagrams, graphs, or equations | |
- Scientific illustrations or charts | |
- Educational content requiring visual analysis | |
- Homework problems or textbook materials | |
I can help analyze and explain any mathematical, scientific, or educational content visible in this image.""" | |
return { | |
"type": "image", | |
"content": description, | |
"base64": img_base64, | |
"metadata": { | |
"width": width, | |
"height": height, | |
"format": format_type, | |
"mode": mode | |
} | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing image: {str(e)}"} | |
def process_document(self, doc_path: str) -> Dict[str, str]: | |
"""Process PDF, DOCX, and text documents""" | |
file_ext = os.path.splitext(doc_path)[1].lower() | |
try: | |
if file_ext == '.pdf': | |
return self.process_pdf(doc_path) | |
elif file_ext in ['.docx', '.doc']: | |
return self.process_docx(doc_path) | |
elif file_ext == '.txt': | |
return self.process_text(doc_path) | |
else: | |
return {"type": "error", "content": "Unsupported document format"} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing document: {str(e)}"} | |
def process_pdf(self, pdf_path: str) -> Dict[str, str]: | |
"""Extract text from PDF files""" | |
try: | |
with open(pdf_path, 'rb') as file: | |
pdf_reader = PyPDF2.PdfReader(file) | |
text_content = "" | |
# Extract text from all pages (limit to first 10 for performance) | |
max_pages = min(10, len(pdf_reader.pages)) | |
for page_num in range(max_pages): | |
page = pdf_reader.pages[page_num] | |
text_content += page.extract_text() + "\n\n" | |
# Truncate if too long | |
if len(text_content) > 5000: | |
text_content = text_content[:5000] + "\n\n[Content truncated for processing...]" | |
analysis = f"""PDF DOCUMENT ANALYSIS: | |
- Total Pages: {len(pdf_reader.pages)} | |
- Pages Processed: {max_pages} | |
- Extracted Text Length: {len(text_content)} characters | |
EXTRACTED CONTENT: | |
{text_content} | |
EDUCATIONAL CONTEXT: | |
I can help you with any questions about this document, including: | |
- Explaining concepts mentioned in the text | |
- Solving problems presented | |
- Summarizing key points | |
- Analyzing educational content""" | |
return { | |
"type": "pdf", | |
"content": analysis, | |
"extracted_text": text_content, | |
"metadata": { | |
"total_pages": len(pdf_reader.pages), | |
"processed_pages": max_pages | |
} | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing PDF: {str(e)}"} | |
def process_docx(self, docx_path: str) -> Dict[str, str]: | |
"""Extract text from DOCX files""" | |
try: | |
doc = docx.Document(docx_path) | |
text_content = "" | |
# Extract text from all paragraphs | |
for paragraph in doc.paragraphs: | |
text_content += paragraph.text + "\n" | |
# Truncate if too long | |
if len(text_content) > 5000: | |
text_content = text_content[:5000] + "\n\n[Content truncated for processing...]" | |
analysis = f"""WORD DOCUMENT ANALYSIS: | |
- Paragraphs: {len(doc.paragraphs)} | |
- Extracted Text Length: {len(text_content)} characters | |
EXTRACTED CONTENT: | |
{text_content} | |
EDUCATIONAL CONTEXT: | |
I can help you with any educational content in this document, including: | |
- Explaining concepts and topics | |
- Answering questions about the material | |
- Providing additional context and examples | |
- Helping with assignments or homework""" | |
return { | |
"type": "docx", | |
"content": analysis, | |
"extracted_text": text_content, | |
"metadata": { | |
"paragraphs": len(doc.paragraphs) | |
} | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing DOCX: {str(e)}"} | |
def process_text(self, txt_path: str) -> Dict[str, str]: | |
"""Process plain text files""" | |
try: | |
with open(txt_path, 'r', encoding='utf-8') as file: | |
text_content = file.read() | |
# Truncate if too long | |
if len(text_content) > 5000: | |
text_content = text_content[:5000] + "\n\n[Content truncated for processing...]" | |
analysis = f"""TEXT FILE ANALYSIS: | |
- File Size: {os.path.getsize(txt_path)} bytes | |
- Character Count: {len(text_content)} | |
- Line Count: {text_content.count(chr(10)) + 1} | |
CONTENT: | |
{text_content} | |
EDUCATIONAL CONTEXT: | |
I can help you with any educational content in this text file.""" | |
return { | |
"type": "text", | |
"content": analysis, | |
"extracted_text": text_content | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing text file: {str(e)}"} | |
def process_data_file(self, data_path: str) -> Dict[str, str]: | |
"""Process CSV and Excel files""" | |
file_ext = os.path.splitext(data_path)[1].lower() | |
try: | |
if file_ext == '.csv': | |
df = pd.read_csv(data_path) | |
elif file_ext in ['.xlsx', '.xls']: | |
df = pd.read_excel(data_path) | |
else: | |
return {"type": "error", "content": "Unsupported data format"} | |
# Basic analysis | |
rows, cols = df.shape | |
columns = list(df.columns) | |
# Sample data (first 5 rows) | |
sample_data = df.head().to_string() | |
# Basic statistics for numeric columns | |
numeric_summary = "" | |
numeric_cols = df.select_dtypes(include=['number']).columns | |
if len(numeric_cols) > 0: | |
numeric_summary = f"\nNUMERIC COLUMN STATISTICS:\n{df[numeric_cols].describe().to_string()}" | |
analysis = f"""DATA FILE ANALYSIS: | |
- Format: {file_ext.upper()} | |
- Dimensions: {rows} rows Γ {cols} columns | |
- Columns: {', '.join(columns[:10])}{'...' if len(columns) > 10 else ''} | |
SAMPLE DATA (First 5 rows): | |
{sample_data} | |
{numeric_summary} | |
EDUCATIONAL CONTEXT: | |
I can help you with: | |
- Data analysis and interpretation | |
- Statistical calculations | |
- Creating visualizations (descriptions) | |
- Understanding data patterns and trends | |
- Homework involving data science""" | |
return { | |
"type": "data", | |
"content": analysis, | |
"dataframe": df, | |
"metadata": { | |
"rows": rows, | |
"columns": cols, | |
"column_names": columns | |
} | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing data file: {str(e)}"} | |
def process_code_file(self, code_path: str) -> Dict[str, str]: | |
"""Process code files""" | |
file_ext = os.path.splitext(code_path)[1].lower() | |
try: | |
with open(code_path, 'r', encoding='utf-8') as file: | |
code_content = file.read() | |
# Truncate if too long | |
if len(code_content) > 3000: | |
code_content = code_content[:3000] + "\n\n[Code truncated for processing...]" | |
# Count lines | |
line_count = code_content.count('\n') + 1 | |
analysis = f"""CODE FILE ANALYSIS: | |
- Language: {file_ext[1:].upper()} | |
- Lines of Code: {line_count} | |
- File Size: {os.path.getsize(code_path)} bytes | |
CODE CONTENT: | |
```{file_ext[1:]} | |
{code_content} | |
``` | |
EDUCATIONAL CONTEXT: | |
I can help you with: | |
- Code explanation and analysis | |
- Debugging and optimization suggestions | |
- Algorithm explanations | |
- Programming concept clarification | |
- Homework and project assistance""" | |
return { | |
"type": "code", | |
"content": analysis, | |
"code": code_content, | |
"language": file_ext[1:], | |
"metadata": { | |
"lines": line_count, | |
"language": file_ext[1:] | |
} | |
} | |
except Exception as e: | |
return {"type": "error", "content": f"Error processing code file: {str(e)}"} | |
class MultimodalDatasetSupremacyAI: | |
"""Enhanced Dataset Supremacy AI with multimodal capabilities""" | |
def __init__(self): | |
# Initialize base dataset system | |
from __main__ import DatasetPoweredRouter | |
self.router = DatasetPoweredRouter() | |
self.groq_url = "https://api.groq.com/openai/v1/chat/completions" | |
# Add multimodal processor | |
self.multimodal = MultimodalProcessor() | |
# Dataset collections (same as before) | |
self.datasets = {} | |
self.examples = {} | |
self.dataset_metadata = {} | |
self.loading_status = "π Loading Multimodal Dataset Supremacy AI..." | |
self.total_examples = 0 | |
# Enhanced analytics | |
self.stats = { | |
"total_queries": 0, | |
"file_uploads": 0, | |
"file_types": {}, | |
"dataset_usage": {}, | |
"model_usage": {}, | |
"subjects": {}, | |
"response_times": [], | |
"multimodal_queries": 0 | |
} | |
# Load datasets (reuse existing logic) | |
self.load_dataset_supremacy() | |
def load_dataset_supremacy(self): | |
"""Load comprehensive educational datasets (same logic as before)""" | |
def load_thread(): | |
try: | |
if not DATASETS_AVAILABLE: | |
self.loading_status = "β Multimodal AI Ready (Premium fallback mode)" | |
self.create_premium_dataset_fallbacks() | |
return | |
self.loading_status = "π₯ Loading Multimodal Dataset Collection..." | |
# Core datasets (simplified for demo) | |
core_datasets = [ | |
("lighteval/MATH", "competition_math", 1500), | |
("meta-math/MetaMathQA", "math_reasoning", 2000), | |
("gsm8k", "basic_math", 2000), | |
("allenai/ai2_arc", "science_reasoning", 1500), | |
("sciq", "science_qa", 1500), | |
("sahil2801/CodeAlpaca-20k", "basic_coding", 1500), | |
("cais/mmlu", "university_knowledge", 1500), | |
("yahma/alpaca-cleaned", "general_education", 2000) | |
] | |
loaded_count = 0 | |
for dataset_name, category, sample_size in core_datasets: | |
try: | |
self.loading_status = f"π Loading {dataset_name}..." | |
if "mmlu" in dataset_name: | |
dataset = load_dataset(dataset_name, "all", split=f"train[:{sample_size}]") | |
else: | |
dataset = load_dataset(dataset_name, split=f"train[:{sample_size}]") | |
processed_examples = self.process_dataset(dataset, category, dataset_name) | |
if processed_examples: | |
self.datasets[category] = dataset | |
self.examples[category] = processed_examples | |
self.dataset_metadata[category] = { | |
"source": dataset_name, | |
"size": len(processed_examples), | |
"quality": 9 | |
} | |
loaded_count += 1 | |
print(f"β {dataset_name} β {len(processed_examples)} examples") | |
except Exception as e: | |
print(f"β οΈ {dataset_name} unavailable: {e}") | |
continue | |
self.total_examples = sum(len(examples) for examples in self.examples.values()) | |
if self.total_examples > 0: | |
self.loading_status = f"β MULTIMODAL AI READY - {loaded_count} datasets, {self.total_examples:,} examples" | |
else: | |
self.loading_status = "β Multimodal AI Ready (Core functionality active)" | |
self.create_premium_dataset_fallbacks() | |
print(f"π Multimodal Dataset Supremacy AI ready with {self.total_examples:,} examples") | |
except Exception as e: | |
self.loading_status = "β Multimodal AI Ready (Fallback mode)" | |
self.create_premium_dataset_fallbacks() | |
print(f"Dataset loading info: {e}") | |
thread = threading.Thread(target=load_thread) | |
thread.daemon = True | |
thread.start() | |
def process_dataset(self, dataset, category, source_name): | |
"""Process datasets (simplified version)""" | |
examples = [] | |
for item in dataset: | |
try: | |
processed = None | |
if category == "competition_math" and item.get('problem') and item.get('solution'): | |
processed = { | |
'question': item['problem'], | |
'solution': item['solution'], | |
'type': 'competition', | |
'subject': 'mathematics', | |
'quality': 10 | |
} | |
elif category in ["math_reasoning", "basic_math"] and item.get('question') and item.get('answer'): | |
processed = { | |
'question': item['question'], | |
'solution': item['answer'], | |
'type': 'math_problem', | |
'subject': 'mathematics', | |
'quality': 9 | |
} | |
elif category in ["science_reasoning", "science_qa"]: | |
if item.get('question') and item.get('correct_answer'): | |
processed = { | |
'question': item['question'], | |
'solution': item['correct_answer'], | |
'type': 'science', | |
'subject': 'science', | |
'quality': 8 | |
} | |
if processed and len(processed['question']) > 20: | |
examples.append(processed) | |
except Exception: | |
continue | |
return examples[:150] # Keep top 150 per category | |
def create_premium_dataset_fallbacks(self): | |
"""Create fallback examples""" | |
self.examples = { | |
'competition_math': [{ | |
'question': 'Prove that β2 is irrational', | |
'solution': 'Assume β2 is rational, so β2 = p/q where p,q are integers with gcd(p,q)=1...', | |
'type': 'proof', | |
'subject': 'mathematics', | |
'quality': 10 | |
}], | |
'basic_math': [{ | |
'question': 'Solve xΒ² - 5x + 6 = 0', | |
'solution': 'Factor: (x-2)(x-3) = 0, so x = 2 or x = 3', | |
'type': 'algebra', | |
'subject': 'mathematics', | |
'quality': 9 | |
}] | |
} | |
self.total_examples = 10 | |
async def educate_multimodal_async(self, question, files=None, subject="general", | |
difficulty="intermediate", language="English"): | |
"""Enhanced education function with multimodal support""" | |
# Analytics tracking | |
self.stats["total_queries"] += 1 | |
self.stats["subjects"][subject] = self.stats["subjects"].get(subject, 0) + 1 | |
start_time = time.time() | |
# Process uploaded files | |
file_context = "" | |
if files and len(files) > 0: | |
self.stats["file_uploads"] += 1 | |
self.stats["multimodal_queries"] += 1 | |
file_analyses = [] | |
for file_path in files: | |
if file_path: # Check if file exists | |
file_result = self.multimodal.process_file(file_path) | |
file_analyses.append(file_result) | |
# Track file types | |
file_type = file_result.get("type", "unknown") | |
self.stats["file_types"][file_type] = self.stats["file_types"].get(file_type, 0) + 1 | |
# Build file context for prompt | |
if file_analyses: | |
file_context = "\n\nFILE ANALYSIS:\n" | |
for i, analysis in enumerate(file_analyses, 1): | |
file_context += f"\nFile {i}:\n{analysis['content']}\n" | |
file_context += "\nPlease consider the uploaded file(s) when answering the question.\n" | |
if not question.strip() and not file_context: | |
return "π Welcome to Multimodal Dataset Supremacy AI! Ask questions and upload files (images, PDFs, documents, data) for enhanced educational assistance!" | |
# Enhanced query analysis considering file context | |
query_type = self.router.analyze_query_complexity(question, subject, difficulty) | |
if file_context and ("image" in file_context.lower() or "pdf" in file_context.lower()): | |
# Boost complexity for multimodal queries | |
if query_type == "quick_facts": | |
query_type = "general" | |
routing_config = self.router.dataset_routing[query_type] | |
selected_model = routing_config["model"] | |
# Track usage | |
self.stats["model_usage"][selected_model] = self.stats["model_usage"].get(selected_model, 0) + 1 | |
self.stats["dataset_usage"][query_type] = self.stats["dataset_usage"].get(query_type, 0) + 1 | |
# Get relevant examples from datasets | |
examples = self.get_optimal_examples(question, query_type, routing_config["examples"]) | |
# Create enhanced prompt with file context and datasets | |
system_prompt = f"""You are a multimodal educational AI enhanced with premium datasets and file processing capabilities. | |
DATASET ENHANCEMENT: | |
You have access to premium educational datasets including competition mathematics, advanced science reasoning, programming excellence, and academic knowledge. | |
""" | |
if examples: | |
system_prompt += "\n\nPREMIUM DATASET EXAMPLES:\n" | |
for i, ex in enumerate(examples, 1): | |
system_prompt += f"\nExample {i}:\nQ: {ex['question'][:200]}...\nA: {ex['solution'][:200]}...\n" | |
system_prompt += f""" | |
MULTIMODAL CAPABILITIES: | |
- I can analyze images, PDFs, documents, spreadsheets, and code files | |
- I provide educational context for all uploaded materials | |
- I combine file analysis with dataset-enhanced responses | |
{file_context} | |
TASK: Provide a comprehensive educational response that: | |
- Uses dataset-quality explanations and examples | |
- Incorporates analysis of any uploaded files | |
- Shows step-by-step reasoning when appropriate | |
- Provides educational context and applications | |
- Subject: {subject} | Difficulty: {difficulty} | |
""" | |
if language != "English": | |
system_prompt += f"\n\nIMPORTANT: Respond in {language}." | |
# Prepare messages | |
user_message = question if question.strip() else "Please analyze and explain the uploaded file(s) from an educational perspective." | |
messages = [ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_message} | |
] | |
try: | |
# Call Groq model | |
response = await self.call_groq_model(selected_model, messages, routing_config["temperature"]) | |
response_time = time.time() - start_time | |
self.stats["response_times"].append(response_time) | |
if response: | |
# Enhanced footer with multimodal info | |
model_name = self.router.models[selected_model]["name"] | |
file_info = f" β’ {len(files)} file(s)" if files and len(files) > 0 else "" | |
footer = f"\n\n---\n*π **{model_name}** enhanced with premium datasets{file_info} β’ {self.total_examples:,} examples β’ {response_time:.2f}s β’ Multimodal Query #{self.stats['multimodal_queries']:,}*" | |
return response + footer | |
else: | |
return "β οΈ Service temporarily unavailable. Please try again." | |
except Exception as e: | |
return f"π§ Technical issue. Please try again." | |
def get_optimal_examples(self, question, query_type, num_examples=2): | |
"""Get relevant examples from datasets""" | |
routing_config = self.router.dataset_routing.get(query_type, self.router.dataset_routing["general"]) | |
target_datasets = routing_config["datasets"] | |
all_examples = [] | |
for dataset_category in target_datasets: | |
if dataset_category in self.examples: | |
all_examples.extend(self.examples[dataset_category]) | |
if not all_examples: | |
for category_examples in self.examples.values(): | |
all_examples.extend(category_examples) | |
if all_examples: | |
return random.sample(all_examples, min(num_examples, len(all_examples))) | |
return [] | |
async def call_groq_model(self, model_id, messages, temperature=0.2): | |
"""Call Groq model""" | |
model_config = self.router.models[model_id] | |
headers = { | |
"Authorization": f"Bearer {self.router.groq_api_key}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"model": model_config["model_id"], | |
"messages": messages, | |
"temperature": temperature, | |
"max_tokens": model_config["max_tokens"] | |
} | |
async with aiohttp.ClientSession() as session: | |
async with session.post(self.groq_url, headers=headers, json=payload, timeout=25) as response: | |
if response.status == 200: | |
result = await response.json() | |
return result["choices"][0]["message"]["content"] | |
else: | |
raise Exception(f"Groq API error: {response.status}") | |
def educate_multimodal(self, question, files=None, subject="general", difficulty="intermediate", language="English"): | |
"""Synchronous wrapper""" | |
try: | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
return loop.run_until_complete( | |
self.educate_multimodal_async(question, files, subject, difficulty, language) | |
) | |
except Exception as e: | |
return f"π§ System error. Please try again." | |
finally: | |
loop.close() | |
def get_multimodal_analytics(self): | |
"""Get comprehensive analytics including multimodal stats""" | |
total = self.stats["total_queries"] | |
multimodal_percent = (self.stats["multimodal_queries"] / total * 100) if total > 0 else 0 | |
file_stats = "" | |
for file_type, count in sorted(self.stats["file_types"].items(), key=lambda x: x[1], reverse=True): | |
file_stats += f"\nβ’ {file_type.title()}: {count} files" | |
analytics = f"""π **MULTIMODAL DATASET SUPREMACY ANALYTICS** | |
π **Performance:** | |
β’ Total Queries: {total:,} | |
β’ Multimodal Queries: {self.stats['multimodal_queries']:,} ({multimodal_percent:.1f}%) | |
β’ File Uploads: {self.stats['file_uploads']:,} | |
β’ Dataset Examples: {self.total_examples:,} | |
π **File Processing:**{file_stats if file_stats else "\nβ’ No files processed yet"} | |
π€ **Model Usage:**""" | |
for model, count in sorted(self.stats["model_usage"].items(), key=lambda x: x[1], reverse=True): | |
model_name = self.router.models[model]["name"] | |
percentage = (count / total * 100) if total > 0 else 0 | |
analytics += f"\nβ’ {model_name}: {count} ({percentage:.1f}%)" | |
analytics += f""" | |
π **Supported Formats:** | |
β’ Images: PNG, JPG, GIF, BMP, WebP | |
β’ Documents: PDF, DOCX, TXT | |
β’ Data: CSV, Excel (XLSX, XLS) | |
β’ Code: Python, JavaScript, Java, C++, HTML | |
π **Status:** {self.loading_status}""" | |
return analytics | |
# Initialize Multimodal Dataset Supremacy AI | |
multimodal_ai = MultimodalDatasetSupremacyAI() | |
def create_multimodal_interface(): | |
"""Create the ultimate multimodal education interface""" | |
with gr.Blocks( | |
theme=gr.themes.Origin(), | |
title="π Multimodal Dataset Supremacy AI - Images + PDFs + Premium Datasets", | |
css=""" | |
.header { | |
text-align: center; | |
background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%); | |
padding: 3rem; | |
border-radius: 20px; | |
margin-bottom: 2rem; | |
box-shadow: 0 15px 35px rgba(0,0,0,0.1); | |
} | |
.multimodal-power { | |
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%); | |
border-radius: 15px; | |
padding: 1.5rem; | |
margin: 1rem 0; | |
} | |
""" | |
) as demo: | |
# Multimodal Header | |
gr.HTML(""" | |
<div class="header"> | |
<h1 style="color: white; margin: 0; font-size: 3.5em; font-weight: 800;">π MULTIMODAL DATASET SUPREMACY AI</h1> | |
<p style="color: #f0f0f0; margin: 1rem 0 0 0; font-size: 1.4em; font-weight: 400;"> | |
Images + PDFs + Documents + Premium Datasets = Ultimate Educational AI | |
</p> | |
<div style="margin-top: 1.5rem;"> | |
<span style="background: rgba(255,255,255,0.25); padding: 0.7rem 1.2rem; border-radius: 25px; margin: 0.3rem; display: inline-block; color: white; font-weight: 600;">π± Images</span> | |
<span style="background: rgba(255,255,255,0.25); padding: 0.7rem 1.2rem; border-radius: 25px; margin: 0.3rem; display: inline-block; color: white; font-weight: 600;">π PDFs</span> | |
<span style="background: rgba(255,255,255,0.25); padding: 0.7rem 1.2rem; border-radius: 25px; margin: 0.3rem; display: inline-block; color: white; font-weight: 600;">π» Code</span> | |
<span style="background: rgba(255,255,255,0.25); padding: 0.7rem 1.2rem; border-radius: 25px; margin: 0.3rem; display: inline-block; color: white; font-weight: 600;">π Datasets</span> | |
</div> | |
</div> | |
""") | |
# Main Interface | |
with gr.Row(): | |
with gr.Column(scale=3): | |
with gr.Group(): | |
# File Upload Section | |
gr.HTML('<h3 style="margin-bottom: 1rem;">π Upload Files (Optional)</h3>') | |
file_upload = gr.Files( | |
label="Upload Images, PDFs, Documents, Data Files, or Code", | |
file_types=[ | |
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", # Images | |
".pdf", ".docx", ".doc", ".txt", # Documents | |
".csv", ".xlsx", ".xls", # Data | |
".py", ".js", ".html", ".css", ".java", ".cpp", ".c" # Code | |
], | |
file_count="multiple" | |
) | |
# Question Input | |
question_input = gr.Textbox( | |
label="π Your Educational Question", | |
placeholder="Ask about uploaded files OR any educational topic. I'll enhance responses with premium datasets!", | |
lines=4, | |
max_lines=10 | |
) | |
with gr.Row(): | |
subject_dropdown = gr.Dropdown( | |
choices=[ | |
"general", "mathematics", "science", "physics", "chemistry", | |
"biology", "computer_science", "programming", "english", | |
"literature", "history", "philosophy", "economics", | |
"engineering", "medicine", "psychology", "data_science" | |
], | |
label="π Subject", | |
value="general", | |
interactive=True | |
) | |
difficulty_dropdown = gr.Dropdown( | |
choices=["beginner", "intermediate", "advanced", "competition", "graduate", "phd"], | |
label="β‘ Level", | |
value="intermediate", | |
interactive=True | |
) | |
language_dropdown = gr.Dropdown( | |
choices=["English", "Spanish", "French", "German", "Chinese", "Japanese", "Portuguese", "Italian"], | |
label="π Language", | |
value="English", | |
interactive=True | |
) | |
submit_btn = gr.Button( | |
"π Get Multimodal Answer", | |
variant="primary", | |
size="lg" | |
) | |
with gr.Column(scale=1): | |
with gr.Group(): | |
gr.HTML('<div class="multimodal-power"><h3>π Multimodal Power Status</h3></div>') | |
analytics_display = gr.Textbox( | |
label="π Multimodal Analytics", | |
value=multimodal_ai.get_multimodal_analytics(), | |
lines=20, | |
interactive=False | |
) | |
refresh_btn = gr.Button("π Refresh Analytics", size="sm") | |
# Response Area | |
answer_output = gr.Textbox( | |
label="π Multimodal Dataset-Enhanced Response", | |
lines=22, | |
max_lines=35, | |
interactive=False, | |
placeholder="Your premium, multimodal, dataset-enhanced educational response will appear here..." | |
) | |
# Multimodal Examples Section | |
with gr.Group(): | |
gr.HTML('<h3 style="text-align: center; margin: 1rem 0;">π Multimodal Dataset Supremacy Examples</h3>') | |
# Text-only examples (dataset-powered) | |
with gr.Accordion("π Dataset-Enhanced Examples (No Files)", open=False): | |
gr.Examples( | |
examples=[ | |
# Competition Mathematics | |
["Prove that there are infinitely many prime numbers using Euclid's method", None, "mathematics", "competition", "English"], | |
["Solve the differential equation dy/dx = xy with initial condition y(0) = 1", None, "mathematics", "advanced", "English"], | |
# Advanced Sciences | |
["Explain the double-slit experiment and its implications for quantum mechanics", None, "physics", "advanced", "English"], | |
["Describe the mechanism of enzyme catalysis using the induced fit model", None, "biology", "advanced", "English"], | |
# Programming | |
["Implement a binary search algorithm and analyze its time complexity", None, "programming", "intermediate", "English"], | |
["Explain object-oriented programming principles with examples", None, "computer_science", "intermediate", "English"], | |
], | |
inputs=[question_input, file_upload, subject_dropdown, difficulty_dropdown, language_dropdown], | |
outputs=answer_output, | |
fn=multimodal_ai.educate_multimodal, | |
cache_examples=False | |
) | |
# Multimodal examples (with file instructions) | |
with gr.Accordion("π Multimodal Examples (Upload Files)", open=True): | |
gr.HTML(""" | |
<div style="padding: 1rem; background: #f8f9fa; border-radius: 10px; margin: 1rem 0;"> | |
<h4>π― Try These Multimodal Scenarios:</h4> | |
<ul style="margin: 0.5rem 0;"> | |
<li><strong>π· Math Problems:</strong> Upload image of handwritten equation β Ask "Solve this step by step"</li> | |
<li><strong>π PDF Analysis:</strong> Upload textbook PDF β Ask "Explain the key concepts in this chapter"</li> | |
<li><strong>π Data Science:</strong> Upload CSV file β Ask "Analyze this data and find patterns"</li> | |
<li><strong>π» Code Review:</strong> Upload Python file β Ask "Explain this code and suggest improvements"</li> | |
<li><strong>π Document Help:</strong> Upload assignment PDF β Ask "Help me understand these problems"</li> | |
<li><strong>πΌοΈ Diagrams:</strong> Upload scientific diagram β Ask "Explain what this illustration shows"</li> | |
</ul> | |
<p style="margin: 0.5rem 0; font-style: italic;">Mix file uploads with dataset-enhanced explanations for ultimate educational power!</p> | |
</div> | |
""") | |
# Event Handlers | |
submit_btn.click( | |
fn=multimodal_ai.educate_multimodal, | |
inputs=[question_input, file_upload, subject_dropdown, difficulty_dropdown, language_dropdown], | |
outputs=answer_output, | |
api_name="predict" | |
) | |
question_input.submit( | |
fn=multimodal_ai.educate_multimodal, | |
inputs=[question_input, file_upload, subject_dropdown, difficulty_dropdown, language_dropdown], | |
outputs=answer_output | |
) | |
refresh_btn.click( | |
fn=multimodal_ai.get_multimodal_analytics, | |
outputs=analytics_display | |
) | |
# Comprehensive Footer | |
gr.HTML(""" | |
<div style="text-align: center; margin-top: 3rem; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 20px; color: white;"> | |
<h3 style="margin-bottom: 1rem; font-size: 1.8em;">π Ultimate Educational AI Architecture</h3> | |
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin: 1.5rem 0;"> | |
<div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 10px;"> | |
<h4 style="margin-bottom: 1rem;">π Multimodal Capabilities</h4> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Images:</strong> PNG, JPG, GIF, BMP, WebP analysis</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Documents:</strong> PDF text extraction, DOCX processing</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Data Files:</strong> CSV, Excel analysis & statistics</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Code Files:</strong> Python, JS, Java, C++ explanation</p> | |
</div> | |
<div style="background: rgba(255,255,255,0.1); padding: 1.5rem; border-radius: 10px;"> | |
<h4 style="margin-bottom: 1rem;">π Dataset Supremacy</h4> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Competition Math:</strong> AMC, AIME, USAMO problems</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Science Reasoning:</strong> University-level science QA</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Programming:</strong> Industry-standard code examples</p> | |
<p style="margin: 0.5rem 0; font-size: 0.9em;"><strong>Academic Knowledge:</strong> Research-quality content</p> | |
</div> | |
</div> | |
<div style="margin: 1.5rem 0; padding: 1rem; background: rgba(255,255,255,0.15); border-radius: 10px;"> | |
<h4 style="margin-bottom: 1rem;">π― Competitive Advantages</h4> | |
<div style="display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem; text-align: left;"> | |
<div> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β 100% Free Operation</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β File Processing</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Premium Datasets</p> | |
</div> | |
<div> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Smart Model Routing</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Multi-language Support</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β K-PhD Coverage</p> | |
</div> | |
<div> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Ultra-fast Groq Speed</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Educational Focus</p> | |
<p style="margin: 0.3rem 0; font-size: 0.9em;">β Scalable Architecture</p> | |
</div> | |
</div> | |
</div> | |
<div style="margin-top: 1.5rem; padding: 1rem; background: rgba(255,255,255,0.1); border-radius: 10px;"> | |
<p style="margin: 0; font-size: 0.9em;"> | |
π <strong>API Endpoint:</strong> https://memoroeisdead-your-education-api.hf.space/run/predict<br> | |
π‘ <strong>Mission:</strong> Prove that premium datasets + file processing beats expensive models<br> | |
π― <strong>Result:</strong> The most advanced, cost-effective educational AI in existence | |
</p> | |
</div> | |
</div> | |
""") | |
return demo | |
if __name__ == "__main__": | |
interface = create_multimodal_interface() | |
interface.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False, | |
show_error=True, | |
show_tips=True, | |
enable_queue=True, | |
max_threads=50 | |
) |