Module: Aura

Defined in:
lib/aura.rb,
lib/aura/docker.rb,
lib/aura/parser.rb,
lib/aura/vercel.rb,
lib/aura/codegen.rb,
lib/aura/emitter.rb,
lib/aura/version.rb,
lib/aura/analyzer.rb,
lib/aura/diagnostics.rb,
lib/aura/transformer.rb

Defined Under Namespace

Modules: Diagnostics, Docker, Nodes, Vercel Classes: Analyzer, CodeGen, DeployError, Emitter, ParseError, Parser, SemanticError, Transformer

Constant Summary collapse

VERSION =

Single source of truth for the framework version. Referenced by the gemspec, the CLI banner, and the generated-code header so they can never drift apart again.

"1.3.0"

Class Method Summary collapse

Class Method Details

.build_docker(filename) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/aura.rb', line 287

def self.build_docker(filename)
  dockerfile = <<~DOCKER
    FROM ruby:3.3
    RUN apt-get update && apt-get install -y libtorch-dev
    WORKDIR /app
    COPY Gemfile* ./
    RUN bundle install
    COPY . .
    EXPOSE 8080
    CMD ["aura", "run", "#{filename}"]
  DOCKER
  File.write("Dockerfile", dockerfile)
  puts "🐳 Dockerfile generated for #{filename}"
end

.parse(source) ⇒ Object



168
169
170
# File 'lib/aura.rb', line 168

def self.parse(source)
  Parser.new.parse(source.gsub(/#.*$/, ""))
end

.run_file(f) ⇒ Object



285
# File 'lib/aura.rb', line 285

def self.run_file(f); eval(transpile(File.read(f))); end

.transpile(source) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/aura.rb', line 172

def self.transpile(source)
  ast = parse(source)
  nodes = Transformer.new.apply(ast).flatten.compact
  
  output = ["# Aura v1.2.0 Advanced (Restored)", "require 'torch'", "require 'sinatra/base'", "require 'json'", "require 'logger'", "require 'net/http'", "require 'uri'", "DEVICE = Torch.cuda_available? ? 'cuda' : 'cpu'"]
  
  routes = nodes.select { |n| n[:type] == :route }

  has_web = nodes.any? { |n| n[:type] == :run_web } || routes.any?

  nodes.each do |n|
    case n[:type]
    when :env
      output << "class AuraConfig"
      output << "  def self.load"
      output << "    {"
      n[:config].each do |item|
        k = item[:key] || item["key"]
        v = item[:value] || item["value"]
        val_str = v.is_a?(String) ? "\"#{v}\"" : v
        output << "      #{k}: #{val_str},"
      end
      output << "    }"
      output << "  end"
      output << "end"
    when :model
      if n[:llm_provider] == "openai"
        output << "class #{n[:name].capitalize}Model"
        output << "  def predict(msg)"
        output << "    api_key = ENV[\"OPENAI_API_KEY\"]"
        output << "    uri = URI('https://api.openai.com/v1/chat/completions')"
        output << "    req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json', 'Authorization' => \"Bearer \#{api_key}\")"
        output << "    req.body = { model: \"#{n[:model_id]}\", messages: [{ role: 'user', content: msg }] }.to_json"
        output << "    res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }"
        output << "    JSON.parse(res.body).dig('choices', 0, 'message', 'content')"
        output << "  end"
        output << "end"
        output << "#{n[:name]}_model = #{n[:name].capitalize}Model.new"
      elsif n[:llm_provider] == "ollama"
        output << "class #{n[:name].capitalize}Model"
        output << "  def predict(msg)"
        output << "    uri = URI('http://localhost:11434/api/generate')"
        output << "    req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')"
        output << "    req.body = { model: \"#{n[:model_id]}\", prompt: msg, stream: false }.to_json"
        output << "    res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }"
        output << "    JSON.parse(res.body)['response']"
        output << "  end"
        output << "end"
        output << "#{n[:name]}_model = #{n[:name].capitalize}Model.new"
      elsif n[:transfer]
        output << "class #{n[:name].capitalize}Model < Torch::NN::Module"
        output << "  def initialize; super; @base = Torchvision::Models.#{n[:transfer]}(pretrained: true); end"
        output << "  def forward(x); @base.call(x); end"
        output << "end"
        output << "#{n[:name]}_model = #{n[:name].capitalize}Model.new.to(DEVICE)"
      else
        output << "class #{n[:name].capitalize}Model < Torch::NN::Module"
        output << "  def initialize"
        output << "    super"
        channels = 1
        n[:layers]&.each_with_index do |l, i|
          if l[:type] == :conv2d
            output << "    @layer#{i} = Torch::NN::Conv2d.new(#{channels}, #{l[:filters]}, #{l[:kernel]})"
            channels = l[:filters]
          end
        end
        output << "  end"
        output << "  def forward(x)"
        n[:layers]&.each_with_index do |l, i|
          if l[:type] == :conv2d
            output << "    x = @layer#{i}.call(x)"
          elsif l[:type] == :flatten
            output << "    x = x.view(x.size(0), -1)"
          end
        end
        output << "    x"
        output << "  end"
        output << "end"
        output << "#{n[:name]}_model = #{n[:name].capitalize}Model.new.to(DEVICE)"
      end
    when :train
      output << "# Advanced Training Loop"
      output << "optimizer = Torch::Optim::#{n[:config][:optimizer] || 'Adam'}.new(#{n[:model]}_model.parameters)"
      if n[:config][:scheduler_type]
         output << "scheduler = Torch::Optim::LRScheduler::#{n[:config][:scheduler_type]}.new(optimizer)"
      end
      epochs = n[:config][:epochs] || 1
      output << "#{epochs}.times do |epoch|"
      output << "  #{n[:model]}_model.train"
      output << "end"
    end
  end

  if has_web
    port_node = nodes.find { |n| n[:type] == :run_web }
    port = port_node ? port_node[:port] : 3000
    output << "class App < Sinatra::Base"
    output << "  set :port, #{port}"
    routes.each do |r|
      output << "  #{r[:method]} '#{r[:path]}' do"
      r[:body]&.each do |line|
        if line[:line] && line[:line][:model]
          output << "    #{line[:line][:model]}_model.call(Torch.tensor(input))"
        end
      end
      output << "  end"
    end
    output << "end"
    output << "App.run!"
  end
  output.join("\n")
end