Spaces:
Paused
Paused
Ali Mohsin
commited on
Commit
·
c03608f
1
Parent(s):
ec83d0c
new updates
Browse files
app.py
CHANGED
|
@@ -1,92 +1,124 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import json
|
| 4 |
-
import random
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# Initialize the client
|
|
|
|
|
|
|
| 8 |
token = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
| 9 |
-
client = InferenceClient("
|
| 10 |
|
| 11 |
-
SYSTEM_PROMPT = """You are ModelForge, an
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
You must analyze the user's request and return a JSON object with the following structure:
|
| 14 |
{
|
| 15 |
"analysis": {
|
| 16 |
"dataType": "image" | "text" | "tabular" | "audio" | "video" | "time_series" | "multimodal",
|
| 17 |
-
"taskType": "classification" | "regression" | "nlp" | "vision" | "forecasting" | "multimodal_reasoning",
|
| 18 |
"complexity": "low" | "medium" | "high" | "research",
|
| 19 |
-
"domain": "string (e.g.,
|
|
|
|
| 20 |
},
|
| 21 |
"recommendations": [
|
| 22 |
{
|
| 23 |
-
"name": "Model Name",
|
| 24 |
-
"description": "
|
| 25 |
-
"pros": ["
|
| 26 |
-
"cons": ["
|
| 27 |
-
"architectureDiagram": "Mermaid graph
|
| 28 |
-
"mlopsBestPractices": [
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
]
|
| 32 |
}
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
|
| 38 |
FEW_SHOT_EXAMPLES = """
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
{
|
| 42 |
-
"analysis": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"recommendations": [
|
| 44 |
{
|
| 45 |
-
"name": "
|
| 46 |
-
"description": "
|
| 47 |
-
"pros": ["
|
| 48 |
-
"cons": ["
|
| 49 |
-
"architectureDiagram": "graph TD\\nA[
|
| 50 |
-
"mlopsBestPractices": [
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
}
|
| 53 |
]
|
| 54 |
}
|
| 55 |
"""
|
| 56 |
|
| 57 |
def generate_solution(description):
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
try:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
temperature=0.7,
|
| 65 |
-
top_p=0.95
|
| 66 |
-
return_full_text=False
|
| 67 |
)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
json_str = json_str.split("```json")[1].split("```")[0].strip()
|
| 73 |
-
elif
|
| 74 |
-
|
| 75 |
-
|
| 76 |
return json_str
|
| 77 |
except Exception as e:
|
| 78 |
return json.dumps({
|
| 79 |
"error": str(e),
|
| 80 |
-
"analysis": {"dataType": "text", "taskType": "nlp", "complexity": "low", "domain": "
|
| 81 |
"recommendations": []
|
| 82 |
})
|
| 83 |
|
| 84 |
demo = gr.Interface(
|
| 85 |
fn=generate_solution,
|
| 86 |
-
inputs=gr.Textbox(lines=5, placeholder="Describe your ML problem..."),
|
| 87 |
outputs=gr.JSON(label="Recommendations"),
|
| 88 |
-
title="ModelForge AI Backend",
|
| 89 |
-
description="
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import json
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Initialize the client
|
| 7 |
+
# We use a powerful instruction-tuned model. Mistral-7B-Instruct-v0.3 is excellent for reasoning.
|
| 8 |
+
# If that fails, we can fallback to others, but let's start strong.
|
| 9 |
token = os.getenv("HUGGING_FACE_HUB_TOKEN")
|
| 10 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3", token=token)
|
| 11 |
|
| 12 |
+
SYSTEM_PROMPT = """You are ModelForge, an elite AI Solutions Architect with deep expertise in MLOps, research-level machine learning, and software engineering.
|
| 13 |
+
|
| 14 |
+
Your goal is to analyze the user's problem and design a **complete, production-ready, and theoretically sound** machine learning solution. You must handle complex, multimodal, and research-level requests with ease.
|
| 15 |
+
|
| 16 |
+
### Output Format
|
| 17 |
+
You must return a SINGLE valid JSON object. Do not include any markdown formatting (like ```json) outside the object.
|
| 18 |
|
|
|
|
| 19 |
{
|
| 20 |
"analysis": {
|
| 21 |
"dataType": "image" | "text" | "tabular" | "audio" | "video" | "time_series" | "multimodal",
|
| 22 |
+
"taskType": "classification" | "regression" | "nlp" | "vision" | "forecasting" | "multimodal_reasoning" | "reinforcement_learning" | "generative",
|
| 23 |
"complexity": "low" | "medium" | "high" | "research",
|
| 24 |
+
"domain": "string (e.g., 'High-Frequency Trading', 'Autonomous Driving', 'Genomic Research')",
|
| 25 |
+
"keyChallenges": ["challenge 1", "challenge 2"]
|
| 26 |
},
|
| 27 |
"recommendations": [
|
| 28 |
{
|
| 29 |
+
"name": "Model/Architecture Name",
|
| 30 |
+
"description": "A comprehensive technical description. For research problems, describe the novel architecture (e.g., 'Dual-Encoder with Cross-Attention Adapters'). For production, specify the exact backbone (e.g., 'ResNet-50v2 with FPN').",
|
| 31 |
+
"pros": ["Critical advantage 1", "Critical advantage 2", "Critical advantage 3"],
|
| 32 |
+
"cons": ["Trade-off 1", "Trade-off 2"],
|
| 33 |
+
"architectureDiagram": "A detailed Mermaid.js graph (graph TD). Use square brackets [] for ALL nodes. NO curly braces {}. Include data ingestion, preprocessing, backbone, heads, and post-processing.",
|
| 34 |
+
"mlopsBestPractices": [
|
| 35 |
+
"Data Versioning: Strategy (e.g., DVC/Delta Lake)",
|
| 36 |
+
"Experiment Tracking: Tools (e.g., MLflow/W&B)",
|
| 37 |
+
"Deployment: Strategy (e.g., Canary, Blue-Green, Edge)",
|
| 38 |
+
"Monitoring: Metrics to watch (e.g., Drift, Latency)",
|
| 39 |
+
"Hardware: Recommended GPU/TPU"
|
| 40 |
+
],
|
| 41 |
+
"trainingCode": "Production-grade Python code snippet (PyTorch/TensorFlow/JAX). Include imports, model definition, and a dummy training loop."
|
| 42 |
}
|
| 43 |
]
|
| 44 |
}
|
| 45 |
|
| 46 |
+
### Guidelines for "Robust & Complex"
|
| 47 |
+
1. **Deep Analysis**: Don't just say "tabular". Say "High-dimensional tabular data with potential covariate shift".
|
| 48 |
+
2. **Novel Architectures**: If the user asks for "latent program synthesis", design a "Neural Module Network with Discrete Latent Variables". Do not recommend generic models for research problems.
|
| 49 |
+
3. **Complete Pipelines**: The MLOps section must be actionable and specific to the problem (e.g., "Use ONNX Runtime for <10ms latency").
|
| 50 |
+
4. **Valid JSON**: Your response must be parseable by `json.loads()`.
|
| 51 |
"""
|
| 52 |
|
| 53 |
FEW_SHOT_EXAMPLES = """
|
| 54 |
+
User Input: "Create a unified multimodal reasoning model that composes and executes latent programs across vision, text, and audio."
|
| 55 |
+
|
| 56 |
+
JSON Response:
|
| 57 |
{
|
| 58 |
+
"analysis": {
|
| 59 |
+
"dataType": "multimodal",
|
| 60 |
+
"taskType": "multimodal_reasoning",
|
| 61 |
+
"complexity": "research",
|
| 62 |
+
"domain": "Neuro-Symbolic AI",
|
| 63 |
+
"keyChallenges": ["Cross-modal alignment", "Differentiable program synthesis", "Latent space collapse"]
|
| 64 |
+
},
|
| 65 |
"recommendations": [
|
| 66 |
{
|
| 67 |
+
"name": "Neuro-Symbolic Latent Programmer",
|
| 68 |
+
"description": "A unified architecture combining a ViT (Vision), RoBERTa (Text), and Wav2Vec2 (Audio) encoder into a shared embedding space. A central 'Program Synthesizer' LSTM decodes discrete symbolic tokens (Map, Filter, Join) which are executed by differentiable neural modules. Uses Gumbel-Softmax for end-to-end training of discrete operations.",
|
| 69 |
+
"pros": ["Interpretable reasoning steps", "Generalizes to new combinations", "End-to-end differentiable"],
|
| 70 |
+
"cons": ["Unstable training dynamics", "High computational cost during search"],
|
| 71 |
+
"architectureDiagram": "graph TD\\nA[Image/Text/Audio Input] --> B[Modality Encoders]\\nB --> C[Shared Latent Space]\\nC --> D[Program Synthesizer LSTM]\\nD --> E[Symbolic Tokens]\\nE --> F[Neural Module Network]\\nF --> G[Execution Result]\\nG --> H[Loss Calculation]",
|
| 72 |
+
"mlopsBestPractices": [
|
| 73 |
+
"Data: WebDataset for sharded multimodal data",
|
| 74 |
+
"Training: Distributed Data Parallel (DDP) on A100 cluster",
|
| 75 |
+
"Monitoring: Track discrete token distribution entropy",
|
| 76 |
+
"Versioning: Git LFS for checkpoints"
|
| 77 |
+
],
|
| 78 |
+
"trainingCode": "import torch\\nimport torch.nn as nn\\n\\nclass NeuroSymbolicModel(nn.Module):\\n def __init__(self):\\n super().__init__()\\n self.vision_enc = VisionTransformer()\\n self.text_enc = BERT()\\n # ..."
|
| 79 |
}
|
| 80 |
]
|
| 81 |
}
|
| 82 |
"""
|
| 83 |
|
| 84 |
def generate_solution(description):
|
| 85 |
+
messages = [
|
| 86 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 87 |
+
{"role": "user", "content": f"User Input: \"{description}\"\n\nProvide the JSON solution:"}
|
| 88 |
+
]
|
| 89 |
|
| 90 |
try:
|
| 91 |
+
# Use chat_completion for conversational models (Zephyr, Mistral, Llama)
|
| 92 |
+
response = client.chat_completion(
|
| 93 |
+
messages,
|
| 94 |
+
max_tokens=2500,
|
| 95 |
temperature=0.7,
|
| 96 |
+
top_p=0.95
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
+
content = response.choices[0].message.content
|
| 100 |
+
|
| 101 |
+
# Robust JSON cleanup
|
| 102 |
+
json_str = content.strip()
|
| 103 |
+
if "```json" in json_str:
|
| 104 |
json_str = json_str.split("```json")[1].split("```")[0].strip()
|
| 105 |
+
elif "```" in json_str:
|
| 106 |
+
json_str = json_str.split("```")[1].split("```")[0].strip()
|
| 107 |
+
|
| 108 |
return json_str
|
| 109 |
except Exception as e:
|
| 110 |
return json.dumps({
|
| 111 |
"error": str(e),
|
| 112 |
+
"analysis": {"dataType": "text", "taskType": "nlp", "complexity": "low", "domain": "error_fallback"},
|
| 113 |
"recommendations": []
|
| 114 |
})
|
| 115 |
|
| 116 |
demo = gr.Interface(
|
| 117 |
fn=generate_solution,
|
| 118 |
+
inputs=gr.Textbox(lines=5, placeholder="Describe your complex ML problem..."),
|
| 119 |
outputs=gr.JSON(label="Recommendations"),
|
| 120 |
+
title="ModelForge AI Backend v2",
|
| 121 |
+
description="Advanced ML Solutions Architect (Powered by Mistral-7B-Instruct)"
|
| 122 |
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|