Anupam251272's picture
Create app.py
5f9a692 verified
import gradio as gr
import numpy as np
import plotly.graph_objects as go
from dataclasses import dataclass
@dataclass
class Project:
name: str
difficulty: str
components: list
description: str
model_data: dict
class VirtualLab3D:
def __init__(self):
self.projects = {
'basic': self._load_basic_projects(),
'intermediate': self._load_intermediate_projects(),
'advanced': self._load_advanced_projects()
}
def _load_basic_projects(self):
return {
'led_circuit': {
'name': 'LED Circuit',
'components': ['LED', 'Resistor', 'Battery'],
'steps': ['Connect resistor to LED', 'Add power source'],
'model': self._create_led_model()
},
'voltage_divider': {
'name': 'Voltage Divider',
'components': ['Resistor x2', 'Power Source'],
'steps': ['Series connection', 'Measure output voltage'],
'model': self._create_voltage_divider_model()
}
}
def _load_intermediate_projects(self):
return {
'arduino_robot': {
'name': 'Arduino Robot Arm',
'components': ['Arduino Uno', 'Servo Motors x3', 'Power Supply'],
'steps': ['Servo wiring', 'Arduino programming', '3D assembly'],
'model': self._create_robot_model()
},
'oscilloscope': {
'name': 'Digital Oscilloscope',
'components': ['MCU', 'ADC', 'Display', 'Amplifier'],
'steps': ['Signal conditioning', 'ADC setup', 'Display interface'],
'model': self._create_oscilloscope_model()
}
}
def _load_advanced_projects(self):
return {
'smart_grid': {
'name': 'Smart Grid Simulator',
'components': ['Power Sources', 'Load Banks', 'Smart Meters'],
'steps': ['Grid topology', 'Power flow analysis', 'Control system'],
'model': self._create_smart_grid_model()
},
'quantum_circuit': {
'name': 'Quantum Circuit Simulator',
'components': ['Quantum Gates', 'Qubits', 'Measurement Units'],
'steps': ['Circuit design', 'Quantum operations', 'State analysis'],
'model': self._create_quantum_model()
}
}
def _create_led_model(self):
# Example 3D model data for an LED circuit
return {
'name': 'LED Circuit',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_voltage_divider_model(self):
# Example 3D model data for a voltage divider
return {
'name': 'Voltage Divider',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_robot_model(self):
# Example 3D model data for a robot arm
return {
'name': 'Arduino Robot Arm',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_oscilloscope_model(self):
# Example 3D model data for an oscilloscope
return {
'name': 'Digital Oscilloscope',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_smart_grid_model(self):
# Example 3D model data for a smart grid
return {
'name': 'Smart Grid Simulator',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_quantum_model(self):
# Example 3D model data for a quantum circuit
return {
'name': 'Quantum Circuit Simulator',
'vertices': np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]]),
'faces': np.array([[0, 1, 2], [0, 2, 3]])
}
def _create_3d_model(self, data):
fig = go.Figure(data=[
go.Mesh3d(
x=data['vertices'][:, 0],
y=data['vertices'][:, 1],
z=data['vertices'][:, 2],
i=data['faces'][:, 0],
j=data['faces'][:, 1],
k=data['faces'][:, 2],
color='lightblue',
opacity=0.7
)
])
fig.update_layout(
scene=dict(
aspectmode='data'
),
title=data['name']
)
return fig
def get_project_info(self, difficulty, project_name):
project = self.projects[difficulty][project_name]
model_data = self._create_3d_model(project['model'])
return {
'name': project['name'],
'components': project['components'],
'steps': project['steps'],
'model': model_data,
'difficulty': difficulty
}
def simulate_project(self, project_data, parameters):
# Simulation logic based on project type
results = {
'voltage': [],
'current': [],
'power': [],
'temperature': []
}
t = np.linspace(0, 10, 100)
if project_data['difficulty'] == 'basic':
results['voltage'] = np.sin(t)
results['current'] = np.cos(t)
elif project_data['difficulty'] == 'intermediate':
results['voltage'] = np.sin(2*t)
results['current'] = np.cos(2*t)
results['temperature'] = 20 + 5*np.sin(t/2)
else:
results['voltage'] = np.sin(3*t)
results['current'] = np.cos(3*t)
results['power'] = np.sin(3*t) * np.cos(3*t)
results['temperature'] = 25 + 10*np.sin(t/3)
return results
def create_interface():
def process_project(difficulty, project_name, parameters):
lab = VirtualLab3D()
project_info = lab.get_project_info(difficulty, project_name)
simulation = lab.simulate_project(project_info, parameters)
return (
project_info['model'],
f"""
πŸ“š Project: {project_info['name']}
πŸ”§ Components Required:
{', '.join(project_info['components'])}
πŸ“ Implementation Steps:
{', '.join(project_info['steps'])}
πŸ“Š Simulation Results:
- Peak Voltage: {max(simulation['voltage']):.2f}V
- Max Current: {max(simulation['current']):.2f}A
- Power Usage: {np.mean(simulation.get('power', [0])):.2f}W
🎯 Learning Objectives:
1. Circuit design principles
2. Component integration
3. System analysis
4. Troubleshooting skills
"""
)
iface = gr.Interface(
fn=process_project,
inputs=[
gr.Dropdown(choices=["basic", "intermediate", "advanced"], label="Difficulty Level"),
gr.Dropdown(choices=["led_circuit", "voltage_divider", "arduino_robot", "oscilloscope", "smart_grid", "quantum_circuit"], label="Project Type"),
gr.Slider(minimum=0, maximum=100, label="Simulation Parameters")
],
outputs=[
gr.Plot(label="3D Model Visualization"),
gr.Textbox(label="Project Details and Analysis")
],
title="πŸ”¬ 3D Virtual Electrical Engineering Lab",
description="Experiment with various electrical projects in a 3D virtual environment"
)
return iface
if __name__ == "__main__":
interface = create_interface()
interface.launch(share=True)