Skip to content

Commit

Permalink
feat: new Models Added
Browse files Browse the repository at this point in the history
  • Loading branch information
MurilonND committed Aug 9, 2023
1 parent a85c565 commit 5ede80e
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 85 deletions.
40 changes: 19 additions & 21 deletions lib/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'package:network_info_plus/network_info_plus.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as shelf_io;

import 'src/features/connection/infrastructure/galaxy_cubit.dart';

HttpServer? server; // Declare a global variable to hold the server instance

void startImageServer(String? imagePath, int lgScreens, Uint8List? imageFileBytes) async {
Expand Down Expand Up @@ -60,18 +58,18 @@ class ImageServer {
body { margin: 0; padding: 0; height: 100vh; display: flex; justify-content: start; align-items: start; }
.logo-container { width: 50vw; height: 50vw; background-image: url('data:image/png;base64, $base64Logo'); background-size: 100% 100%; position: absolute; z-index: 1; }
.image-container { width: 100%; height: 100%; background-image: url('data:image/png;base64, $base64Image'); background-size: ${_getImageWight(lgScreens)} 100%; }
.image-container.image3 { background-position: 0 0; }
.image-container.image1 { background-position: 50% 0; }
.image-container.image2 { background-position: 100% 0; }
.image-container.5 { background-position: 0 0; }
.image-container.3 { background-position: 25% 0; }
.image-container.1 { background-position: 50% 0; }
.image-container.2 { background-position: 75% 0; }
.image-container.4 { background-position: 100% 0; }
.image-container.image33 { background-position: 0 0; }
.image-container.image31 { background-position: 50% 0; }
.image-container.image32 { background-position: 100% 0; }
.image-container.image55 { background-position: 0 0; }
.image-container.image53 { background-position: 25% 0; }
.image-container.image51 { background-position: 50% 0; }
.image-container.image52 { background-position: 75% 0; }
.image-container.image54 { background-position: 100% 0; }
</style>
</head>
<body>
<div class="${_getLogoClass(request.url.path)}"></div>
<div class="${_getLogoClass(request.url.path, lgScreens)}"></div>
<div class="image-container ${_getImageClass(request.url.path, lgScreens)}"></div>
</body>
</html>
Expand All @@ -84,8 +82,8 @@ class ImageServer {
}
}

String _getLogoClass(String path){
if(path == '3' || path == '5'){
String _getLogoClass(String path, int lgScreens){
if(path == lgScreens.toString()){
return 'logo-container';
}
return '';
Expand All @@ -103,26 +101,26 @@ class ImageServer {
if(lgScreens == 5){
switch (path) {
case '1':
return '1';
return 'image51';
case '2':
return '2';
return 'image52';
case '3':
return '3';
return 'image53';
case '4':
return '4';
return 'image54';
case '5':
return '5';
return 'image55';
default:
return '';
}
}else{
switch (path) {
case '1':
return 'image1';
return 'image31';
case '2':
return 'image2';
return 'image32';
case '3':
return 'image3';
return 'image33';
default:
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ class _ApiKeyRepositoryPageState extends State<ApiKeyRepositoryPage> {
var dallEController = TextEditingController();
var ipAddressLocalMachineController = TextEditingController();
var portLocalMachineController = TextEditingController();

var leapController = TextEditingController();

@override
void initState() {
_galaxyCubit = context.read<GalaxyCubit>();

dallEController = TextEditingController(text: _galaxyCubit.state.dalleKey);
ipAddressLocalMachineController = TextEditingController(text: _galaxyCubit.state.ipAddressLocalMachine);
portLocalMachineController = TextEditingController(text: _galaxyCubit.state.portLocalMachine);
ipAddressLocalMachineController =
TextEditingController(text: _galaxyCubit.state.ipAddressLocalMachine);
portLocalMachineController =
TextEditingController(text: _galaxyCubit.state.portLocalMachine);
leapController = TextEditingController(text: _galaxyCubit.state.leapKey);

super.initState();
}
Expand All @@ -59,19 +62,31 @@ class _ApiKeyRepositoryPageState extends State<ApiKeyRepositoryPage> {
crossAxisAlignment: CrossAxisAlignment.center,
children: [
const Text(
"Do you want to save those information's inside your device?", style: TextStyle(fontSize: 20),),
const SizedBox(height: 20,),
"Do you want to save those information's inside your device?",
style: TextStyle(fontSize: 20),
),
const SizedBox(
height: 20,
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
LiquidArtButton(
label: 'Save',
onTap: () async {
await UserConfigurations.setDallEKey(dallEController.text);
onTap: () {
UserConfigurations.setDallEKey(dallEController.text);
UserConfigurations.setLeapKey(leapController.text);
UserConfigurations.setIpAddressLocalMachine(
ipAddressLocalMachineController.text);
UserConfigurations.setPortLocalMachine(
portLocalMachineController.text);
Navigator.of(context).pop();
},
),
const SizedBox(width: 10,),
const SizedBox(
width: 10,
),
LiquidArtButton(
label: 'Cancel',
onTap: () {
Expand Down Expand Up @@ -115,6 +130,17 @@ class _ApiKeyRepositoryPageState extends State<ApiKeyRepositoryPage> {
const SizedBox(
height: 20,
),
LiquidArtTextField(
label: 'Leap API key',
hintText: '',
textController: leapController,
onChanged: (value) {
_galaxyCubit.leapKeyChanged(value);
},
),
const SizedBox(
height: 20,
),
LiquidArtTextField(
label: 'Ip Address of the Docker Machine',
hintText: '172.16.51.173',
Expand Down
7 changes: 7 additions & 0 deletions lib/src/features/connection/infrastructure/galaxy_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class GalaxyCubit extends Cubit<GalaxyState> {
emit(newState);
}

void leapKeyChanged(String value) {
final newState = state.copyWith(
leapKey: value,
);
emit(newState);
}

void passwordChanged(String value) {
final newState = state.copyWith(
password: value,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/features/connection/infrastructure/galaxy_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class GalaxyState extends Equatable {
final int lgScreens;
final String? errorMessage;
final String dalleKey;
final String leapKey;
final String ipAddressLocalMachine;
final String portLocalMachine;

Expand All @@ -25,6 +26,7 @@ class GalaxyState extends Equatable {
this.lgScreens = 3,
this.errorMessage,
this.dalleKey = '',
this.leapKey = '',
this.ipAddressLocalMachine = '',
this.portLocalMachine = '',
});
Expand All @@ -43,6 +45,7 @@ class GalaxyState extends Equatable {
lgScreens,
errorMessage,
dalleKey,
leapKey,
ipAddressLocalMachine,
portLocalMachine,
];
Expand All @@ -58,6 +61,7 @@ class GalaxyState extends Equatable {
int? lgScreens,
String? errorMessage,
String? dalleKey,
String? leapKey,
String? ipAddressLocalMachine,
String? portLocalMachine,
}) {
Expand All @@ -72,6 +76,7 @@ class GalaxyState extends Equatable {
lgScreens: lgScreens ?? this.lgScreens,
errorMessage: errorMessage ?? this.errorMessage,
dalleKey: dalleKey ?? this.dalleKey,
leapKey: leapKey ?? this.leapKey,
ipAddressLocalMachine: ipAddressLocalMachine ?? this.ipAddressLocalMachine,
portLocalMachine: portLocalMachine ?? this.portLocalMachine
);
Expand Down
108 changes: 81 additions & 27 deletions lib/src/features/drawer/infrastructure/api_services.dart
Original file line number Diff line number Diff line change
@@ -1,63 +1,117 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:liquid_art_ai/src/utils/snack_bar_messager.dart';
import 'package:liquid_art_ai/src/utils/user_configurations.dart';

class DallE {
static final url = Uri.parse("https://api.openai.com/v1/images/generations");

static generateImage(context, String imagePrompt, String size, String apiKey) async{
static generateImage(
context, String imagePrompt, String size, String apiKey) async {
final headers = {
"Content-Type": "application/json",
"Authorization": "Bearer $apiKey"
};

final body = jsonEncode(
{
"prompt": imagePrompt,
"n": 1,
"size": size
},
{"prompt": imagePrompt, "n": 1, "size": size},
);

var res = await http.post(
url,
headers: headers,
body: body,
);

var res = await http.post(url, headers: headers, body: body,);
print(res);
if(res.statusCode == 200){
if (res.statusCode == 200) {
WarningSnackBar.apiCallSuccess(context);
var data = jsonDecode(res.body.toString());
print(res);
return data['data'][0]['url'].toString();
}else{
} else {
WarningSnackBar.apiCallError(context, res.body.toString());
}
}
}

class StableDiffusion {
static generateImage(context ,String imagePrompt, String size, String ipAddressLocalMachine, String portLocalMachine) async{
final url = Uri.parse("http://$ipAddressLocalMachine:$portLocalMachine/sdapi/v1/txt2img");

static generateImage(context, String imagePrompt, String size,
String ipAddressLocalMachine, String portLocalMachine) async {
final url = Uri.parse(
"http://$ipAddressLocalMachine:$portLocalMachine/sdapi/v1/txt2img");

final height = int.parse(size.split("x").first);
final width = int.parse(size.split("x").last);

final body = jsonEncode(
{
"prompt": imagePrompt,
"width": width,
"height": height
}
);
final body =
jsonEncode({"prompt": imagePrompt, "width": width, "height": height});

var res = await http.post(url, body: body,);
if(res.statusCode == 200){
var res = await http.post(
url,
body: body,
);
if (res.statusCode == 200) {
WarningSnackBar.apiCallSuccess(context);
var data = jsonDecode(res.body.toString());
return data['images'][0].toString();
}else{
} else {
var data = jsonDecode(res.body.toString());
WarningSnackBar.apiCallError(context, data.toString());
}
}
}

class Leap {
static generateImage(context, String imagePrompt, String size, String apiKey, String modelId) async {
final url = Uri.parse(
"https://api.tryleap.ai/api/v1/images/models/$modelId/inferences");

final height = int.parse(size.split("x").first);
final width = int.parse(size.split("x").last);

final headers = {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Bearer $apiKey"
};

final body = jsonEncode({
"prompt": imagePrompt,
"steps": 50,
"width": width,
"height": height,
"numberOfImages": 1,
"promptStrength": 7,
"enhancePrompt": false,
"restoreFaces": true,
"upscaleBy": "x1"
});

var res = await http.post(
url,
headers: headers,
body: body,
);
var data = jsonDecode(res.body.toString());
var inferenceId = data["id"];

if (data['status'] == 'queued') {
WarningSnackBar.apiCallSuccess(context);

final url = Uri.parse(
"https://api.tryleap.ai/api/v1/images/models/$modelId/inferences/$inferenceId");

var res = await http.get(url, headers: headers);
var data = jsonDecode(res.body.toString());

while (data['state'].toString() != "finished") {
res = await http.get(url, headers: headers);
data = jsonDecode(res.body.toString());
}

var image = data["images"][0]['uri'].toString();

return image;
} else {
WarningSnackBar.apiCallError(context, data.toString());
}
}
}
}
Loading

0 comments on commit 5ede80e

Please sign in to comment.