Styled summary

This commit is contained in:
M66B 2024-06-14 20:55:56 +02:00
parent 4ac3307a5b
commit 58fa1285fe
3 changed files with 12 additions and 10 deletions

View File

@ -165,7 +165,7 @@ public class AI {
return context.getString(R.string.title_summarize);
}
static String getSummaryText(Context context, EntityMessage message) throws JSONException, IOException {
static Spanned getSummaryText(Context context, EntityMessage message) throws JSONException, IOException {
File file = message.getFile(context);
if (!file.exists())
return null;
@ -183,6 +183,7 @@ public class AI {
HtmlHelper.truncate(d, MAX_SUMMARIZE_TEXT_SIZE);
StringBuilder sb = new StringBuilder();
if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", OpenAI.DEFAULT_MODEL);
float temperature = prefs.getFloat("openai_temperature", OpenAI.DEFAULT_TEMPERATURE);
@ -208,7 +209,6 @@ public class AI {
OpenAI.Message[] completions =
OpenAI.completeChat(context, model, input.toArray(new OpenAI.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder();
for (OpenAI.Message completion : completions)
for (OpenAI.Content content : completion.getContent())
if (OpenAI.CONTENT_TEXT.equals(content.getType())) {
@ -216,7 +216,6 @@ public class AI {
sb.append('\n');
sb.append(content.getContent());
}
return sb.toString();
} else if (Gemini.isAvailable(context)) {
String model = prefs.getString("gemini_model", Gemini.DEFAULT_MODEL);
float temperature = prefs.getFloat("gemini_temperature", Gemini.DEFAULT_TEMPERATURE);
@ -235,15 +234,17 @@ public class AI {
Gemini.Message[] completions =
Gemini.generate(context, model, new Gemini.Message[]{content}, temperature, 1);
StringBuilder sb = new StringBuilder();
for (Gemini.Message completion : completions)
for (String result : completion.getContent()) {
if (sb.length() != 0)
sb.append('\n');
sb.append(result);
}
return sb.toString();
} else
throw new IllegalArgumentException("No AI available");
String html = Markdown.toHtml(sb.toString());
Document doc = HtmlHelper.sanitizeCompose(context, html, false);
return HtmlHelper.fromDocument(context, doc, null, null);
}
}

View File

@ -1572,7 +1572,7 @@ public class EntityRule {
}
try {
message.preview = AI.getSummaryText(context, message);
message.preview = AI.getSummaryText(context, message).toString();
} catch (Throwable ex) {
message.error = Log.formatThrowable(ex);
db.message().setMessageError(message.id, message.error);

View File

@ -26,6 +26,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.Spanned;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
@ -87,7 +88,7 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
tvFrom.setText(args.getString("from"));
tvSubject.setText(args.getString("subject"));
new SimpleTask<String>() {
new SimpleTask<Spanned>() {
@Override
protected void onPreExecute(Bundle args) {
tvSummary.setVisibility(View.GONE);
@ -103,7 +104,7 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
protected Spanned onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB db = DB.getInstance(context);
@ -112,14 +113,14 @@ public class FragmentDialogSummarize extends FragmentDialogBase {
return null;
long start = new Date().getTime();
String summary = AI.getSummaryText(context, message);
Spanned summary = AI.getSummaryText(context, message);
args.putLong("elapsed", new Date().getTime() - start);
return summary;
}
@Override
protected void onExecuted(Bundle args, String summary) {
protected void onExecuted(Bundle args, Spanned summary) {
tvSummary.setText(summary);
tvSummary.setVisibility(View.VISIBLE);
ibCopy.setVisibility(View.VISIBLE);