Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddhPurohit committed May 22, 2024
1 parent 1744fef commit 2157951
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 509 deletions.
74 changes: 37 additions & 37 deletions example/cloudflare_ai_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,118 +4,118 @@ import 'package:cloudflare_ai/cloudflare_ai.dart';
import 'package:cloudflare_ai/src/text_chat/text_chat.dart';

void main() async {
String accountId = "Your Account ID";
String apiKey = "Your API Key";
String accountId = "Your Account ID"; // Your Account ID
String apiKey = "Your API Key"; // Your API Key

// Text Generation
// Initialize a TextGenerationModel
TextGenerationModel textGenModel = TextGenerationModel(
accountId: accountId,
apiKey: apiKey,
model: TextGenerationModels.GEMMA_7B_IT,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextGenerationModels.GEMMA_7B_IT, // Model to use
);

// Generate Text for a prompt
TextGenerationResponse textGenRes = await textGenModel
.generateText("Write a story about a robot, living on the moon");
.generateText("Write a story about a robot, living on the moon"); // Prompt

if (textGenRes.success && textGenRes.result != null) {
print(textGenRes.result?.response);
print(textGenRes.result?.response); // Print the generated text if request is successful
} else {
print(textGenRes..errors.map((e) => e.toJson()).toList());
print(textGenRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
}

// Text Summarization
// Initialize a TextSummarizationModel
TextSummarizationModel textSummarizationModel = TextSummarizationModel(
accountId: accountId,
apiKey: apiKey,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextSummarizationModels
.BART_LARGE_CNN, // Bart Large CNN is default, hence this parameter is optional
);

// Summarize Text
TextSummarizationResponse textSummarizationRes =
await textSummarizationModel.summarize(
"Your very long text....",
"Your very long text....", // Text to summarize
maxLength: 1024, // 1024 is default, hence this parameter is optional
);

if (textSummarizationRes.success && textSummarizationRes.result != null) {
print(textSummarizationRes.result?.response);
print(textSummarizationRes.result?.response); // Print the summarized text
} else {
print(textSummarizationRes..errors.map((e) => e.toJson()).toList());
print(textSummarizationRes..errors.map((e) => e.toJson()).toList()); // Print the errors
}

// Text to Image
// Initialize a TextToImageModel
TextToImageModel textToImageModel = TextToImageModel(
accountId: accountId,
apiKey: apiKey,
model: TextToImageModels.DREAMSHAPER_8_LCM,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextToImageModels.DREAMSHAPER_8_LCM, // Define the model
);

// Generate Image
Uint8List textToImageResult =
await textToImageModel.generateImage("An alien on the moon");
await textToImageModel.generateImage("An alien on the moon"); // Prompt

// Save the image to a file
File("image.png").writeAsBytes(textToImageResult);
File("image.png").writeAsBytes(textToImageResult); // Save the image to a file

// Text Classification
// Initialize a TextClassificationModel
TextClassificationModel textClassificationModel = TextClassificationModel(
accountId: accountId,
apiKey: apiKey,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextClassificationModels
.DISTILBERT_SST_2_INT8 // DISTILBERT_SST_2_INT8 is default, hence this parameter is optional
);

// Classify Text
TextClassificationResponse textClassificationResponse =
await textClassificationModel.classifyText(
"Test Prompt",
);
"Test Prompt", // Text to classify
); // Classify the text

if (textClassificationResponse.success &&
textClassificationResponse.result != null) {
print(
'Positive Confidence level: ${textClassificationResponse.result?.positive}');
'Positive Confidence level: ${textClassificationResponse.result?.positive}'); // Print the positive confidence level if request is successful
print(
'Negative Confidence level: ${textClassificationResponse.result?.negative}');
'Negative Confidence level: ${textClassificationResponse.result?.negative}'); // Print the negative confidence level if request is successful
} else {
print(textSummarizationRes..errors.map((e) => e.toJson()).toList());
print(textSummarizationRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
}

// Language Translation
// Initialize a LanguageTranslationModel
LanguageTranslationModel languageTranslationModel = LanguageTranslationModel(
accountId: accountId,
apiKey: apiKey,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: LanguageTranslationModels
.M2M100_1_2B, // M2M100_1_2B is default, hence this parameter is optional
);

// Translate Text
LanguageTranslationResponse languageTranslationRes =
await languageTranslationModel.translate(
"Hello",
Languages.English,
Languages.Hindi,
"Hello", // Text to translate
Languages.English, // Language to translate from
Languages.Hindi, // Language to translate to
);

if (languageTranslationRes.success && languageTranslationRes.result != null) {
print(languageTranslationRes.result?.response);
print(languageTranslationRes.result?.response); // Print the translated text if request is successful
} else {
print(languageTranslationRes..errors.map((e) => e.toJson()).toList());
print(languageTranslationRes..errors.map((e) => e.toJson()).toList()); // Print the errors if request is unsuccessful
}

// Text Chat
// Initialize a TextChatModel
TextChatModel textChatModel = TextChatModel(
accountId: accountId,
apiKey: apiKey,
model: TextChatModels.GEMMA_7B_IT,
accountId: accountId, // Account ID
apiKey: apiKey, // API Key
model: TextChatModels.GEMMA_7B_IT, // Model to use
);

// Load any previous conversations
Expand All @@ -131,7 +131,7 @@ void main() async {
]);

// Send a new message
ChatMessage chatRes = await textChatModel.chat("Who are you?");
ChatMessage chatRes = await textChatModel.chat("Who are you?"); // Message

print(chatRes.content);
print(chatRes.content); // Print the response
}
31 changes: 15 additions & 16 deletions lib/src/language_translation/language_translation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'response.dart';
import '../services/network_service.dart';
import 'models.dart';
import 'languages.dart';

export 'models.dart';
export 'response.dart';
export 'languages.dart';
Expand All @@ -11,37 +10,37 @@ class LanguageTranslationModel {
late String accountId; // Account ID, available on Cloudflare daskboard
late String apiKey; // API Key, can by generated from the Cloudflare dashboard
late LanguageTranslationModels model; // The model to use
NetworkService networkService = NetworkService();
late String baseUrl;
NetworkService networkService = NetworkService(); // Network service object
late String baseUrl; // Base URL for the API

LanguageTranslationModel({
required this.accountId,
required this.apiKey,
required this.accountId,
required this.apiKey,
this.model = LanguageTranslationModels.M2M100_1_2B,
}) {
baseUrl = "https://api.cloudflare.com/client/v4/accounts/$accountId/ai/run";
if (accountId.trim() == "") {
throw Exception("Account ID cannot be empty");
throw Exception("Account ID cannot be empty"); // Throw an exception if account ID is empty
}
if (apiKey.trim() == "") {
throw Exception("API Key cannot be empty");
throw Exception("API Key cannot be empty"); // Throw an exception if API key is empty
}
}

// Asynchronous function which returns summarized text through the TextSummarizationResponse object
Future<LanguageTranslationResponse> translate(
String text,
Languages from,
Languages to,
String text, // Text to translate
Languages from, // Language to translate from
Languages to, // Language to translate to
) async {
Map<String, dynamic> res =
await networkService.post("$baseUrl/${model.value}", apiKey, {
"source_lang": from.value,
"target_lang": to.value,
"text": text,
});
"source_lang": from.value, // Source language
"target_lang": to.value, // Target language
"text": text, // Text to translate
}); // Post request to the API
LanguageTranslationResponse response =
LanguageTranslationResponse.fromJson(res['data']);
return response;
LanguageTranslationResponse.fromJson(res['data']); // Create a response object from the JSON data
return response; // Return the response object
}
}
Loading

0 comments on commit 2157951

Please sign in to comment.