below is my code I am trying to take an audio input and sending it to gemini api which would scan the audio and tell me its tags and genre I need a solution with getting error in uri
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
import 'package:http/http.dart' as http;
import 'package:google_generative_ai/google_generative_ai.dart';
void main() {
runApp(MusicAnalyzerApp());
}
class MusicAnalyzerApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Gemini Music Analyzer',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MusicAnalyzerHome(),
);
}
}
class MusicAnalyzerHome extends StatefulWidget {
@override
_MusicAnalyzerHomeState createState() => _MusicAnalyzerHomeState();
}
class _MusicAnalyzerHomeState extends State<MusicAnalyzerHome> {
String? _filePath;
String? _musicAnalysis;
bool _isLoading = false;
Future<void> _pickFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles(type: FileType.audio);
if (result != null) {
setState(() {
_filePath = result.files.single.path;
});
} else {
// User canceled the picker
}
}
Future<void> _analyzeMusic() async {
if (_filePath == null) return;
setState(() {
_isLoading = true;
_musicAnalysis = null;
});
final apiKey = 'apikey';
final fileId = await uploadFileToGemini(apiKey, _filePath!);
if (fileId == null) {
setState(() {
_isLoading = false;
});
_showErrorDialog('File upload failed');
return;
}
final response = await analyzeMusicWithGemini(apiKey, fileId);
setState(() {
_isLoading = false;
_musicAnalysis = response;
});
}
Future<String?> uploadFileToGemini(String apiKey, String filePath) async {
final uri = Uri.parse('https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent?key=apikey'); // Replace with actual upload endpoint
final request = http.MultipartRequest('POST', uri)
..headers['Authorization'] = 'Bearer $apiKey'
..files.add(await http.MultipartFile.fromPath('file', filePath));
final response = await request.send();
if (response.statusCode == 200) {
final responseBody = await response.stream.bytesToString();
final jsonResponse = jsonDecode(responseBody);
return jsonResponse['fileId']; // Adjust based on the actual response structure
} else {
return null;
}
}
Future<String?> analyzeMusicWithGemini(String apiKey, String fileId) async {
final model = GenerativeModel(
model: 'gemini-1.5-pro',
apiKey: apiKey,
generationConfig: GenerationConfig(
temperature: 1,
topK: 64,
topP: 0.95,
maxOutputTokens: 8192,
responseMimeType: 'text/plain',
),
);
final chat = model.startChat(history: [
Content.multi([
FilePart(uri: fileId), // **Here I am getting error with uri undefined**
TextPart('answer two questions 1 - what kind of music genre and tag it showsn2 - what kind of image genre and tag it shows'),
]),
]);
final response = await chat.sendMessage(Content.text('Analyze the music and image tags'));
return response.text;
}
void _showErrorDialog(String message) {
showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Error'),
content: Text(message),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Gemini Music Analyzer'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
ElevatedButton(
onPressed: _pickFile,
child: const Text('Pick an MP3 File'),
),
const SizedBox(height: 16),
if (_filePath != null)
Text('Selected file: ${_filePath!.split('/').last}'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _analyzeMusic,
child: const Text('Analyze Music'),
),
const SizedBox(height: 16),
if (_isLoading)
const CircularProgressIndicator(),
if (_musicAnalysis != null)
Expanded(
child: SingleChildScrollView(
child: Text(_musicAnalysis!),
),
),
],
),
),
);
}
}
I tried everything I can and was expecting not to get an error I was thinking that it would be model name but dont know if its correct