Refactoring

pull/215/head
M66B 8 months ago
parent 10211970fb
commit dc63ec78fd

@ -106,7 +106,6 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
d = HtmlHelper.sanitizeView(context, d, false);
HtmlHelper.removeSignatures(d);
d.select("blockquote").remove();
HtmlHelper.truncate(d, HtmlHelper.MAX_TRANSLATABLE_TEXT_SIZE);
if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
@ -116,46 +115,9 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
List<OpenAI.Message> input = new ArrayList<>();
input.add(new OpenAI.Message(OpenAI.USER,
new OpenAI.Content[]{new OpenAI.Content(OpenAI.CONTENT_TEXT, prompt)}));
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
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();
Log.i("MMM " + start + "..." + end + " len=" + ssb.length() + " text=" + text.replace('\n', '|'));
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 s = ssb.getSpanStart(spans[0]);
int e = ssb.getSpanEnd(spans[0]);
String src = spans[0].getSource();
String url = null;
if (src.startsWith("cid:")) {
String cid = '<' + src.substring(4) + '>';
EntityAttachment attachment = db.attachment().getAttachment(id, cid);
if (attachment != null && attachment.available)
try {
url = ImageHelper.getDataUri(attachment.getFile(context), attachment.type);
} catch (Throwable ex) {
Log.w(ex);
}
} else
url = src;
Log.i("MMM image " + s + "..." + e + " url=" + url);
if (url != null)
contents.add(new OpenAI.Content(OpenAI.CONTENT_IMAGE, url));
end = e;
}
}
start = end;
}
input.add(new OpenAI.Message(OpenAI.USER, contents.toArray(new OpenAI.Content[0])));
SpannableStringBuilder ssb = HtmlHelper.fromDocument(context, d, null, null);
input.add(new OpenAI.Message(OpenAI.USER, OpenAI.getContent(ssb, id, context)));
OpenAI.Message[] result =
OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1);

@ -22,6 +22,7 @@ package eu.faircode.email;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.Spannable;
import android.text.TextUtils;
import androidx.annotation.NonNull;
@ -258,6 +259,44 @@ public class OpenAI {
return sb.toString();
}
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)
try {
url = ImageHelper.getDataUri(attachment.getFile(context), attachment.type);
} 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;

Loading…
Cancel
Save