Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/src/main/assets/katex/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013-2020 Khan Academy and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions app/src/main/assets/katex/katex.min.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/src/main/assets/katex/katex.min.js

Large diffs are not rendered by default.

150 changes: 145 additions & 5 deletions app/src/main/java/io/github/gohoski/numai/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import io.github.gohoski.numai.api.ApiError;
import io.github.gohoski.numai.api.ApiResult;
import io.github.gohoski.numai.api.ApiService;
import io.github.gohoski.numai.api.GeminiImageResult;
import io.github.gohoski.numai.api.GeminiImageService;
import io.github.gohoski.numai.data.ChatManager;
import io.github.gohoski.numai.data.ConfigManager;
import io.github.gohoski.numai.data.MessageManager;
Expand All @@ -65,6 +67,7 @@
import io.github.gohoski.numai.search.SearchResult;
import io.github.gohoski.numai.search.WebFetcher;
import io.github.gohoski.numai.ui.MessageAdapter;
import io.github.gohoski.numai.util.Base64;
import io.github.gohoski.numai.util.SSLDisabler;

public class MainActivity extends Activity {
Expand All @@ -89,6 +92,7 @@ public class MainActivity extends Activity {
private static final StringBuilder matchingTagBuffer = new StringBuilder();

private ApiService apiService;
private GeminiImageService geminiImageService;
private ConfigManager config;

private ListView msgList;
Expand All @@ -97,13 +101,15 @@ public class MainActivity extends Activity {
private MessageAdapter adapter;
private ImageButton sendBtn;
private ToggleButton thinkingToggle;
private ToggleButton imageToggle;
private ProgressBar progressBar;
private TextView imgCount;
private boolean autoScroll = true;
int UPDATE_DELAY_MS = 250;

private ImageButton attachBtn;
private final List<String> inputImages = new ArrayList<String>();
private long nextImageId = System.currentTimeMillis();

private static class StreamToolCall {
String id = "";
Expand Down Expand Up @@ -152,12 +158,14 @@ protected void onCreate(Bundle savedInstanceState) {
}

apiService = new ApiService(this);
geminiImageService = new GeminiImageService(this);
msgList = (ListView) findViewById(R.id.messages_list);
helloLayout = findViewById(R.id.hello_layout);
input = (EditText) findViewById(R.id.message_input);
sendBtn = (ImageButton) findViewById(R.id.send_button);
attachBtn = (ImageButton) findViewById(R.id.attach_button);
thinkingToggle = (ToggleButton) findViewById(R.id.thinking);
imageToggle = (ToggleButton) findViewById(R.id.image_generation);
progressBar = (ProgressBar) findViewById(R.id.waiting);
imgCount = (TextView) findViewById(R.id.img_count);

Expand All @@ -175,10 +183,21 @@ public void onClick(View v) {
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// Use the raw extra name so the app can keep its API 1 compatibility.
// Android 4.3+ file pickers return multiple selections through ClipData.
intent.putExtra("android.intent.extra.ALLOW_MULTIPLE", true);
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)), REQUEST_CODE_PICK_IMAGE);
}
});

imageToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
updateImageModeUi();
}
});
updateImageModeUi();

adapter = new MessageAdapter(this, MessageManager.getInstance().getMessages());
msgList.setAdapter(adapter);
scrollToBottom();
Expand Down Expand Up @@ -489,12 +508,39 @@ private void updateEmptyState() {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_IMAGE && resultCode == RESULT_OK && data != null) {
processSelectedImage(data.getData());
boolean processedMultiple = processSelectedImages(data);
if (!processedMultiple && data.getData() != null) {
processSelectedImage(data.getData());
}
}
}

private boolean processSelectedImages(Intent data) {
try {
Method getClipData = data.getClass().getMethod("getClipData", new Class[0]);
Object clipData = getClipData.invoke(data, new Object[0]);
if (clipData == null) return false;

Method getItemCount = clipData.getClass().getMethod("getItemCount", new Class[0]);
Method getItemAt = clipData.getClass().getMethod("getItemAt", new Class[]{Integer.TYPE});
int itemCount = ((Integer) getItemCount.invoke(clipData, new Object[0])).intValue();

for (int i = 0; i < itemCount; i++) {
Object item = getItemAt.invoke(clipData, new Object[]{Integer.valueOf(i)});
Method getUri = item.getClass().getMethod("getUri", new Class[0]);
Uri uri = (Uri) getUri.invoke(item, new Object[0]);
if (uri != null) processSelectedImage(uri);
}
return itemCount > 0;
} catch (Exception ignored) {
// ClipData does not exist on old Android versions. The single-image
// data URI fallback in onActivityResult keeps the old behavior working.
return false;
}
}

private void processSelectedImage(Uri uri) {
String fileName = "img_" + System.currentTimeMillis() + ".jpg";
String fileName = "img_" + nextImageId++ + ".jpg";
FileOutputStream fos = null;
boolean success = false;
try {
Expand Down Expand Up @@ -596,13 +642,19 @@ private void stopGeneration() {

private void sendMessage() {
String text = input.getText().toString().trim();
final boolean generateImage = imageToggle != null && imageToggle.isChecked();
if (generateImage && text.length() == 0) {
Toast.makeText(this, R.string.gemini_image_prompt_required, Toast.LENGTH_SHORT).show();
return;
}
if (text.length() == 0 && inputImages.isEmpty()) return;
if (isGenerating) {
stopGeneration();
}
hideKeyboard();
autoScroll = true;
MessageManager.getInstance().addMessage(new Message(Role.USER, text, new ArrayList<String>(inputImages), null));
final List<String> selectedImages = new ArrayList<String>(inputImages);
MessageManager.getInstance().addMessage(new Message(Role.USER, text, selectedImages, null));
ChatManager.getInstance().onMessageAdded(this);
input.setText("");
sendBtn.setImageResource(R.drawable.ic_action_stop);
Expand All @@ -614,7 +666,11 @@ private void sendMessage() {
adapter.notifyDataSetChanged();
updateEmptyState();
scrollToBottom();
requestAICompletion();
if (generateImage) {
requestGeminiImage(text, selectedImages);
} else {
requestAICompletion();
}
}

private void requestAICompletion() {
Expand Down Expand Up @@ -656,6 +712,90 @@ public void run() {
});
}

private void requestGeminiImage(final String prompt, final List<String> selectedImages) {
isThinkingState = false;
isGenerating = true;
currentAssistantMsg = null;
isThinkingEnabled = false;
globalCancelled = false;
final int genId = ++globalGenerationId;

geminiImageService.generate(prompt, selectedImages, new ApiCallback<GeminiImageResult>() {
@Override
public void onSuccess(final GeminiImageResult result) {
if (genId != globalGenerationId || globalCancelled) return;
runOnCurrentActivity(new Runnable() {
@Override
public void run() {
if (genId != globalGenerationId || globalCancelled) return;
MainActivity act = currentActivityInstance;
if (act != null) act.saveGeminiImageResult(result);
}
});
}

@Override
public void onError(final ApiError error) {
if (genId != globalGenerationId || globalCancelled) return;
runOnCurrentActivity(new Runnable() {
@Override
public void run() {
if (genId != globalGenerationId || globalCancelled) return;
MainActivity act = currentActivityInstance;
if (act != null) act.handleStreamError(error.getMessage());
}
});
}
});
}

private void saveGeminiImageResult(GeminiImageResult result) {
String mimeType = result.getMimeType();
boolean isPng = "image/png".equalsIgnoreCase(mimeType);
String fileName = "gemini_img_" + nextImageId++ + (isPng ? ".png" : ".jpg");
FileOutputStream fos = null;
boolean saved = false;
try {
byte[] bytes = Base64.decode(result.getImageData());
fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(bytes);
fos.flush();
saved = bytes.length > 0;
} catch (Exception e) {
Log.e("GeminiImage", "Could not save generated image", e);
} finally {
if (fos != null) {
try { fos.close(); } catch (IOException ignored) {}
}
}

if (!saved) {
deleteFile(fileName);
handleStreamError(getString(R.string.gemini_image_save_failed));
return;
}

String description = result.getText();
if (description == null || description.trim().length() == 0) {
description = getString(R.string.gemini_image_done);
}
Message generated = new Message(Role.ASSISTANT, description, getString(R.string.gemini_image_model_label));
generated.setOutputImage(fileName);
MessageManager.getInstance().addMessage(generated);
ChatManager.getInstance().onMessageAdded(this);
resetUIState();
}

private void updateImageModeUi() {
if (imageToggle == null || thinkingToggle == null || input == null) return;
boolean generatingImage = imageToggle.isChecked();
if (generatingImage) {
thinkingToggle.setChecked(false);
}
thinkingToggle.setEnabled(!generatingImage);
input.setHint(generatingImage ? R.string.gemini_image_prompt_hint : R.string.ask_anything);
}

private void startResponseStream(final int genId, final InputStream stream, String model, final boolean thinkingEnabled) {
if (genId != globalGenerationId || globalCancelled) {
if (stream != null) {
Expand Down Expand Up @@ -1273,4 +1413,4 @@ private String extractJSONReasoning(JSONObject delta) {

return null;
}
}
}
Loading