-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
106 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
class ApiKeys { | ||
static const ninja = "MqP1o6xY70Aq40AyhCkZ6g==Ydp5zzxo1Zr5qSUB"; | ||
static const huggingFace = "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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:translator/translator.dart'; | ||
|
||
import '../../models/recipe/recipe.dart'; | ||
import '../keys/ninja.dart'; | ||
|
||
class HuggingFace { | ||
static final HuggingFace _instance = HuggingFace._internal(); | ||
|
||
factory HuggingFace() { | ||
return _instance; | ||
} | ||
|
||
HuggingFace._internal(); | ||
|
||
static const modelsUrl = "https://api-inference.huggingface.co/models/"; | ||
|
||
Uri _getModel(String modelName) { | ||
return Uri.parse(modelsUrl + modelName); | ||
} | ||
|
||
Future? getRecipe(List<String> ingredients) async { | ||
try { | ||
String translatedIngredients = | ||
(await ingredients.join(",").translate()).text; | ||
|
||
var r = await http.post( | ||
_getModel("flax-community/t5-recipe-generation"), | ||
headers: _getHds, | ||
body: translatedIngredients, | ||
); | ||
|
||
return Recipe.from(jsonDecode(r.body)[0]["generated_text"]); | ||
} catch (e) { | ||
print(e); | ||
return "Une erreur est survenue \n$e"; | ||
} | ||
} | ||
|
||
Map<String, String> get _getHds { | ||
return { | ||
'Authorization': "Bearer ${ApiKeys.huggingFace}", | ||
"Accept": "*/*", | ||
"Content-Type": "application/json", | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
class Recipe { | ||
String? title; | ||
String? ingredients; | ||
String? servings; | ||
String? instructions; | ||
String title; | ||
String ingredients; | ||
String directions; | ||
|
||
Recipe({ | ||
this.title, | ||
this.ingredients, | ||
this.servings, | ||
this.instructions, | ||
required this.title, | ||
required this.ingredients, | ||
required this.directions, | ||
}); | ||
|
||
Map<String, dynamic> toJson() => { | ||
'title': title, | ||
'ingredients': ingredients, | ||
'servings': servings, | ||
'instructions': instructions, | ||
}; | ||
factory Recipe.from(String text) { | ||
List<String> results = ["", "", ""]; | ||
|
||
List<String> words = text.split(" "); | ||
int index = 0; | ||
for (var word in words) { | ||
if (word == "title:") { | ||
index = 0; | ||
} else if (word == "ingredients:") { | ||
index = 1; | ||
} else if (word == "directions:") { | ||
index = 2; | ||
} | ||
if (["title:", "ingredients:", "directions:"].contains(word)) { | ||
continue; | ||
} | ||
results[index] += "$word "; | ||
} | ||
|
||
factory Recipe.fromJson(Map<String, dynamic> json) { | ||
return Recipe( | ||
title: json['title'], | ||
ingredients: json['ingredients'], | ||
servings: json['servings'], | ||
instructions: json['instructions'], | ||
title: results[0], | ||
ingredients: results[1], | ||
directions: results[2], | ||
); | ||
} | ||
|
||
bool containsIngredient(String word) { | ||
return ingredients!.contains(word) || | ||
instructions!.contains(word) || | ||
servings!.contains(word) || | ||
title!.contains(word); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.