admin管理员组

文章数量:1124564

I'm trying to build a flutter chat app by running gemma locally on my device, but I am unable to find clear documentations or video examples for it. All I get now is different exceptions without proper details. like

_Exception (Exception: Platform error: Failed to get gemma response).

I've just created a test application and this is the code:

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_gemma/flutter_gemma.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const GemmaPage(),
    );
  }
}

class GemmaPage extends StatefulWidget {
  const GemmaPage({super.key});

  @override
  State<GemmaPage> createState() => _GemmaPageState();
}

class _GemmaPageState extends State<GemmaPage> {
  bool _isModelInitialized = false;
  int? _loadingProgress;
  String? _error;

  Future<void> _initializeModel() async {
    log('entering _initializeModel');
    bool isLoaded = await FlutterGemmaPlugin.instance.isLoaded;
    log("isloaded: $isLoaded");
    if (!isLoaded) {
      log('loading model');
      await for (int progress in FlutterGemmaPlugin.instance
          .loadAssetModelWithProgress(fullPath: 'model.bin')) {
        setState(() {
          _loadingProgress = progress;
          log("progress: $progress");
        });
      }
    }

    log('initializing model');
    await FlutterGemmaPlugin.instance.init(
      maxTokens: 512,
      temperature: 1.0,
      topK: 1,
      randomSeed: 1,
    );
    setState(() {
      _isModelInitialized = true;
    });
  }

  @override
  void initState() {
    _initializeModel();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text("Gemma Response:"),
            const SizedBox(height: 10), // Added spacing between widgets
            ElevatedButton(
              onPressed: () async {
                final flutterGemma = FlutterGemmaPlugin.instance;
                String? response = await flutterGemma.getResponse(
                  prompt: 'Tell me something interesting',
                );

                log(response ?? "empty");
              },
              child: const Text("Test Gemma"),
            ),
          ],
        ),
      ),
    );
  }
}

本文标签: