mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-28 16:55:56 +00:00
Refactoring
This commit is contained in:
parent
73097569f5
commit
63e8bd160c
3 changed files with 45 additions and 45 deletions
|
@ -2678,7 +2678,7 @@ public class FragmentCompose extends FragmentBase {
|
|||
|
||||
OpenAI.Message message;
|
||||
if (body instanceof Spannable)
|
||||
message = new OpenAI.Message(OpenAI.USER, OpenAI.getContent((Spannable) body, id, context));
|
||||
message = new OpenAI.Message(OpenAI.USER, OpenAI.Content.get((Spannable) body, id, context));
|
||||
else
|
||||
message = new OpenAI.Message(OpenAI.USER, new OpenAI.Content[]{
|
||||
new OpenAI.Content(OpenAI.CONTENT_TEXT, body.toString())});
|
||||
|
|
|
@ -117,7 +117,7 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
|
|||
new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, prompt)}));
|
||||
|
||||
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
|
||||
input.add(new OpenAI.Message(OpenAI.USER, OpenAI.getContent(ssb, id, context)));
|
||||
input.add(new OpenAI.Message(OpenAI.USER, OpenAI.Content.get(ssb, id, context)));
|
||||
|
||||
OpenAI.Message[] result =
|
||||
OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1);
|
||||
|
|
|
@ -247,49 +247,6 @@ public class OpenAI {
|
|||
}
|
||||
}
|
||||
|
||||
static Content[] getContent(Spannable ssb, long id, Context context) {
|
||||
DB db = DB.getInstance(context);
|
||||
List<OpenAI.Content> contents = new ArrayList<>();
|
||||
int start = 0;
|
||||
while (start < ssb.length()) {
|
||||
int end = ssb.nextSpanTransition(start, ssb.length(), ImageSpanEx.class);
|
||||
String text = ssb.subSequence(start, end).toString();
|
||||
contents.add(new OpenAI.Content(OpenAI.CONTENT_TEXT, text));
|
||||
if (end < ssb.length()) {
|
||||
ImageSpanEx[] spans = ssb.getSpans(end, end, ImageSpanEx.class);
|
||||
if (spans.length == 1) {
|
||||
int e = ssb.getSpanEnd(spans[0]);
|
||||
String src = spans[0].getSource();
|
||||
|
||||
String url = null;
|
||||
if (src != null && src.startsWith("cid:")) {
|
||||
String cid = '<' + src.substring(4) + '>';
|
||||
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
|
||||
if (attachment != null && attachment.available) {
|
||||
File file = attachment.getFile(context);
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
Bitmap bm = ImageHelper.getScaledBitmap(is, null, null, SCALE2PIXELS);
|
||||
Helper.ByteArrayInOutStream bos = new Helper.ByteArrayInOutStream();
|
||||
bm.compress(Bitmap.CompressFormat.PNG, 90, bos);
|
||||
url = ImageHelper.getDataUri(bos.getInputStream(), "image/png");
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
}
|
||||
}
|
||||
} else
|
||||
url = src;
|
||||
|
||||
if (url != null)
|
||||
contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url));
|
||||
end = e;
|
||||
}
|
||||
}
|
||||
start = end;
|
||||
}
|
||||
|
||||
return contents.toArray(new OpenAI.Content[0]);
|
||||
}
|
||||
|
||||
static class Content {
|
||||
private String type;
|
||||
private String content;
|
||||
|
@ -306,6 +263,49 @@ public class OpenAI {
|
|||
public String getContent() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
static Content[] get(Spannable ssb, long id, Context context) {
|
||||
DB db = DB.getInstance(context);
|
||||
List<OpenAI.Content> contents = new ArrayList<>();
|
||||
int start = 0;
|
||||
while (start < ssb.length()) {
|
||||
int end = ssb.nextSpanTransition(start, ssb.length(), ImageSpanEx.class);
|
||||
String text = ssb.subSequence(start, end).toString();
|
||||
contents.add(new OpenAI.Content(OpenAI.CONTENT_TEXT, text));
|
||||
if (end < ssb.length()) {
|
||||
ImageSpanEx[] spans = ssb.getSpans(end, end, ImageSpanEx.class);
|
||||
if (spans.length == 1) {
|
||||
int e = ssb.getSpanEnd(spans[0]);
|
||||
String src = spans[0].getSource();
|
||||
|
||||
String url = null;
|
||||
if (src != null && src.startsWith("cid:")) {
|
||||
String cid = '<' + src.substring(4) + '>';
|
||||
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
|
||||
if (attachment != null && attachment.available) {
|
||||
File file = attachment.getFile(context);
|
||||
try (InputStream is = new FileInputStream(file)) {
|
||||
Bitmap bm = ImageHelper.getScaledBitmap(is, null, null, SCALE2PIXELS);
|
||||
Helper.ByteArrayInOutStream bos = new Helper.ByteArrayInOutStream();
|
||||
bm.compress(Bitmap.CompressFormat.PNG, 90, bos);
|
||||
url = ImageHelper.getDataUri(bos.getInputStream(), "image/png");
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
}
|
||||
}
|
||||
} else
|
||||
url = src;
|
||||
|
||||
if (url != null)
|
||||
contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url));
|
||||
end = e;
|
||||
}
|
||||
}
|
||||
start = end;
|
||||
}
|
||||
|
||||
return contents.toArray(new OpenAI.Content[0]);
|
||||
}
|
||||
}
|
||||
|
||||
static class Message {
|
||||
|
|
Loading…
Reference in a new issue