Added AI summarization

pull/215/head
M66B 8 months ago
parent 98326a520e
commit 93835d886b

@ -478,6 +478,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private ImageButton ibSearchText;
private ImageButton ibSearch;
private ImageButton ibTranslate;
private ImageButton ibSummarize;
private ImageButton ibFullScreen;
private ImageButton ibForceLight;
private ImageButton ibImportance;
@ -927,6 +928,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchText = vsBody.findViewById(R.id.ibSearchText);
ibSearch = vsBody.findViewById(R.id.ibSearch);
ibTranslate = vsBody.findViewById(R.id.ibTranslate);
ibSummarize = vsBody.findViewById(R.id.ibSummarize);
ibFullScreen = vsBody.findViewById(R.id.ibFullScreen);
ibForceLight = vsBody.findViewById(R.id.ibForceLight);
ibImportance = vsBody.findViewById(R.id.ibImportance);
@ -1099,6 +1101,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearch.setOnClickListener(this);
ibTranslate.setOnClickListener(this);
ibTranslate.setOnLongClickListener(this);
ibSummarize.setOnClickListener(this);
ibFullScreen.setOnClickListener(this);
ibForceLight.setOnClickListener(this);
ibImportance.setOnClickListener(this);
@ -1221,6 +1224,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearch.setOnClickListener(null);
ibTranslate.setOnClickListener(null);
ibTranslate.setOnLongClickListener(null);
ibSummarize.setOnClickListener(null);
ibFullScreen.setOnClickListener(null);
ibForceLight.setOnClickListener(null);
ibImportance.setOnClickListener(null);
@ -1839,6 +1843,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchText.setVisibility(View.GONE);
ibSearch.setVisibility(View.GONE);
ibTranslate.setVisibility(View.GONE);
ibSummarize.setVisibility(View.GONE);
ibFullScreen.setVisibility(View.GONE);
ibForceLight.setVisibility(View.GONE);
ibImportance.setVisibility(View.GONE);
@ -2135,6 +2140,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchText.setVisibility(View.GONE);
ibSearch.setVisibility(View.GONE);
ibTranslate.setVisibility(View.GONE);
ibSummarize.setVisibility(View.GONE);
ibFullScreen.setVisibility(View.GONE);
ibForceLight.setVisibility(View.GONE);
ibImportance.setVisibility(View.GONE);
@ -2349,6 +2355,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean button_hide = prefs.getBoolean("button_hide", false);
boolean button_importance = prefs.getBoolean("button_importance", false);
boolean button_translate = prefs.getBoolean("button_translate", true);
boolean button_summarize = prefs.getBoolean("button_summarize", false);
boolean button_full_screen = prefs.getBoolean("button_full_screen", false);
boolean button_force_light = prefs.getBoolean("button_force_light", true);
boolean button_search = prefs.getBoolean("button_search", false);
@ -2389,6 +2396,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibSearchText.setVisibility(tools && !outbox && button_search_text && message.content ? View.VISIBLE : View.GONE);
ibSearch.setVisibility(tools && !outbox && button_search && (froms > 0 || tos > 0) ? View.VISIBLE : View.GONE);
ibTranslate.setVisibility(tools && !outbox && button_translate && DeepL.isAvailable(context) && message.content ? View.VISIBLE : View.GONE);
ibSummarize.setVisibility(tools && !outbox && button_summarize && (OpenAI.isAvailable(context) || Gemini.isAvailable(context)) && message.content ? View.VISIBLE : View.GONE);
ibFullScreen.setVisibility(tools && full && button_full_screen && message.content ? View.VISIBLE : View.GONE);
ibForceLight.setVisibility(tools && full && dark && button_force_light && message.content ? View.VISIBLE : View.GONE);
ibForceLight.setImageLevel(!(canDarken || fake_dark) || force_light ? 1 : 0);
@ -4529,6 +4537,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
onSearchContact(message, false);
} else if (id == R.id.ibTranslate) {
onActionTranslate(message);
} else if (id == R.id.ibSummarize) {
onActionSummarize(message);
} else if (id == R.id.ibFullScreen)
onActionOpenFull(message);
else if (id == R.id.ibForceLight) {
@ -6239,6 +6249,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
popupMenu.getMenu().findItem(R.id.menu_search_in_text).setEnabled(message.content && !full);
popupMenu.getMenu().findItem(R.id.menu_translate).setVisible(
DeepL.isAvailable(context) && message.content);
popupMenu.getMenu().findItem(R.id.menu_summarize).setVisible(
(OpenAI.isAvailable(context) || Gemini.isAvailable(context)) && message.content);
popupMenu.getMenu().findItem(R.id.menu_force_light).setVisible(full && dark);
popupMenu.getMenu().findItem(R.id.menu_force_light).setChecked(force_light);
@ -6348,6 +6360,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} else if (itemId == R.id.menu_translate) {
onActionTranslate(message);
return true;
} else if (itemId == R.id.menu_summarize) {
onActionSummarize(message);
return true;
} else if (itemId == R.id.menu_force_light) {
onActionForceLight(message);
return true;
@ -7256,6 +7271,15 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
}
private void onActionSummarize(TupleMessageEx message) {
Bundle args = new Bundle();
args.putLong("id", message.id);
FragmentDialogSummarize fragment = new FragmentDialogSummarize();
fragment.setArguments(args);
fragment.show(parentFragment.getParentFragmentManager(), "message:summary");
}
private void onActionForceLight(TupleMessageEx message) {
if (canDarken || fake_dark) {
boolean force_light = !properties.getValue("force_light", message.id);

@ -57,6 +57,7 @@ public class FragmentDialogButtons extends FragmentDialogBase {
final CheckBox cbSearch = dview.findViewById(R.id.cbSearch);
final CheckBox cbSearchText = dview.findViewById(R.id.cbSearchText);
final CheckBox cbTranslate = dview.findViewById(R.id.cbTranslate);
final CheckBox cbSummarize = dview.findViewById(R.id.cbSummarize);
final CheckBox cbFullScreen = dview.findViewById(R.id.cbFullScreen);
final CheckBox cbForceLight = dview.findViewById(R.id.cbForceLight);
final CheckBox cbEvent = dview.findViewById(R.id.cbEvent);
@ -70,6 +71,7 @@ public class FragmentDialogButtons extends FragmentDialogBase {
final CheckBox cbAnswer = dview.findViewById(R.id.cbAnswer);
cbTranslate.setVisibility(DeepL.isAvailable(context) ? View.VISIBLE : View.GONE);
cbSummarize.setVisibility(OpenAI.isAvailable(context) || Gemini.isAvailable(context) ? View.VISIBLE : View.GONE);
cbPin.setVisibility(Shortcuts.can(context) ? View.VISIBLE : View.GONE);
cbSeen.setChecked(prefs.getBoolean("button_seen", false));
@ -87,6 +89,7 @@ public class FragmentDialogButtons extends FragmentDialogBase {
cbSearch.setChecked(prefs.getBoolean("button_search", false));
cbSearchText.setChecked(prefs.getBoolean("button_search_text", false));
cbTranslate.setChecked(prefs.getBoolean("button_translate", true));
cbSummarize.setChecked(prefs.getBoolean("button_summarize", false));
cbFullScreen.setChecked(prefs.getBoolean("button_full_screen", false));
cbForceLight.setChecked(prefs.getBoolean("button_force_light", true));
cbEvent.setChecked(prefs.getBoolean("button_event", false));
@ -126,6 +129,7 @@ public class FragmentDialogButtons extends FragmentDialogBase {
editor.putBoolean("button_search", cbSearch.isChecked());
editor.putBoolean("button_search_text", cbSearchText.isChecked());
editor.putBoolean("button_translate", cbTranslate.isChecked());
editor.putBoolean("button_summarize", cbSummarize.isChecked());
editor.putBoolean("button_full_screen", cbFullScreen.isChecked());
editor.putBoolean("button_force_light", cbForceLight.isChecked());
editor.putBoolean("button_event", cbEvent.isChecked());

@ -0,0 +1,154 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ArrowKeyMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import org.jsoup.nodes.Document;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.mail.Address;
public class FragmentDialogSummarize extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Context context = getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_summarize, null);
final TextView tvSummary = view.findViewById(R.id.tvSummary);
final ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean compact = prefs.getBoolean("compact", false);
int zoom = prefs.getInt("view_zoom", compact ? 0 : 1);
int message_zoom = prefs.getInt("message_zoom", 100);
float textSize = Helper.getTextSize(context, zoom) * message_zoom / 100f;
tvSummary.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
tvSummary.setText(null);
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
pbWait.setVisibility(View.GONE);
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
File file = EntityMessage.getFile(context, id);
Document d = JsoupEx.parse(file);
d = HtmlHelper.sanitizeView(context, d, false);
HtmlHelper.removeSignatures(d);
d.select("blockquote").remove();
HtmlHelper.truncate(d, HtmlHelper.MAX_TRANSLATABLE_TEXT_SIZE);
String text = d.text();
if (OpenAI.isAvailable(context)) {
String model = prefs.getString("openai_model", "gpt-3.5-turbo");
float temperature = prefs.getFloat("openai_temperature", 0.5f);
List<OpenAI.Message> result = new ArrayList<>();
result.add(new OpenAI.Message("assistant", OpenAI.SUMMARY_PROMPT));
result.add(new OpenAI.Message("user", text));
OpenAI.Message[] completions =
OpenAI.completeChat(context, model, result.toArray(new OpenAI.Message[0]), temperature, 1);
StringBuilder sb = new StringBuilder();
for (OpenAI.Message completion : completions) {
if (sb.length() != 0)
sb.append('\n');
sb.append(completion.getContent());
}
return sb.toString();
} else if (Gemini.isAvailable(context)) {
String model = prefs.getString("gemini_model", "gemini-pro");
String[] result = Gemini.generate(context, model, new String[]{Gemini.SUMMARY_PROMPT, text});
return TextUtils.join("\n", result);
}
return null;
}
@Override
protected void onExecuted(Bundle args, String text) {
tvSummary.setText(text);
}
@Override
protected void onException(Bundle args, Throwable ex) {
tvSummary.setText(new ThrowableWrapper(ex).toSafeString());
}
}.execute(this, getArguments(), "message:summarize");
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(view)
.setPositiveButton(android.R.string.cancel, null);
return builder.create();
}
}

@ -19,7 +19,6 @@ package eu.faircode.email;
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
@ -42,6 +41,8 @@ import javax.net.ssl.HttpsURLConnection;
public class Gemini {
// https://ai.google.dev/models/gemini
static final String SUMMARY_PROMPT = "Summarize the following text:";
private static final int MAX_GEMINI_LEN = 4000; // characters
private static final int TIMEOUT = 30; // seconds

@ -43,6 +43,8 @@ import java.util.Objects;
import javax.net.ssl.HttpsURLConnection;
public class OpenAI {
static final String SUMMARY_PROMPT = "Summarize the following text:";
private static final int MAX_OPENAI_LEN = 1000; // characters
private static final int TIMEOUT = 45; // seconds

@ -187,6 +187,19 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbImportance" />
<CheckBox
android:id="@+id/cbSummarize"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:drawableEnd="@drawable/twotone_smart_toy_24"
android:drawablePadding="6dp"
android:text="@string/title_summarize"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbTranslate" />
<CheckBox
android:id="@+id/cbFullScreen"
android:layout_width="0dp"
@ -198,7 +211,7 @@
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cbTranslate" />
app:layout_constraintTop_toBottomOf="@id/cbSummarize" />
<CheckBox
android:id="@+id/cbForceLight"

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:padding="24dp"
android:scrollbarStyle="outsideOverlay">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableStart="@drawable/twotone_smart_toy_24"
android:drawablePadding="6dp"
android:text="@string/title_summarize"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvSummary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:focusable="false"
android:minHeight="60dp"
android:text="Text"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvCaption" />
<eu.faircode.email.ContentLoadingProgressBar
android:id="@+id/pbWait"
style="@style/Base.Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:indeterminate="true"
app:layout_constraintBottom_toBottomOf="@id/tvSummary"
app:layout_constraintEnd_toEndOf="@id/tvSummary"
app:layout_constraintStart_toStartOf="@id/tvSummary"
app:layout_constraintTop_toTopOf="@id/tvSummary"
app:show_delay="0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

@ -50,7 +50,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
app:constraint_referenced_ids="vwEmpty,ibMore,ibInbox,ibJunk,ibTrash,ibArchive,ibMove,ibCopy,ibKeywords,ibLabels,ibNotes,ibSeen,ibHide,ibImportance,ibTranslate,ibFullScreen,ibForceLight,ibSearch,ibSearchText,ibEvent,ibShare,ibPin,ibPrint,ibHeaders,ibHtml,ibRaw,ibUnsubscribe,ibRule,ibAnswer,ibUndo"
app:constraint_referenced_ids="vwEmpty,ibMore,ibInbox,ibJunk,ibTrash,ibArchive,ibMove,ibCopy,ibKeywords,ibLabels,ibNotes,ibSeen,ibHide,ibImportance,ibTranslate,ibSummarize,ibFullScreen,ibForceLight,ibSearch,ibSearchText,ibEvent,ibShare,ibPin,ibPrint,ibHeaders,ibHtml,ibRaw,ibUnsubscribe,ibRule,ibAnswer,ibUndo"
app:flow_horizontalBias="0"
app:flow_horizontalGap="3dp"
app:flow_horizontalStyle="packed"
@ -263,6 +263,18 @@
app:srcCompat="@drawable/twotone_translate_24"
tools:ignore="MissingConstraints" />
<ImageButton
android:id="@+id/ibSummarize"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/title_summarize"
android:padding="6dp"
android:scaleType="fitCenter"
android:tooltipText="@string/title_summarize"
app:srcCompat="@drawable/twotone_smart_toy_24"
tools:ignore="MissingConstraints" />
<ImageButton
android:id="@+id/ibFullScreen"
android:layout_width="36dp"

@ -86,6 +86,11 @@
android:icon="@drawable/twotone_translate_24"
android:title="@string/title_translate" />
<item
android:id="@+id/menu_summarize"
android:icon="@drawable/twotone_smart_toy_24"
android:title="@string/title_summarize" />
<item
android:id="@+id/menu_force_light"
android:checkable="true"

@ -1767,6 +1767,7 @@
<string name="title_create_template">Create template</string>
<string name="title_select_default_identity">Select default address</string>
<string name="title_translate">Translate</string>
<string name="title_summarize">Summarize</string>
<string name="title_openai" translatable="false">OpenAI (ChatGPT)</string>
<string name="title_translate_configure">Configure &#8230;</string>
<string name="title_translate_key">Enter key</string>

Loading…
Cancel
Save