Skip to content

Commit

Permalink
add demo project
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyboyBrian committed Jul 30, 2024
1 parent fa2fd7c commit a9fd805
Show file tree
Hide file tree
Showing 81 changed files with 3,671 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Ocotpus-v2-demo
# Android ChatBot with Octopus v2

This project is an Android-based chatbot application that utilizes the Octopus v2 for generating responses. It is built upon the foundation of the [Android-ChatBot](https://github.com/gangulwar/Android-ChatBot) repository.

## Features

- Android-based chat interface
- Integration with the Octopus v2 model
- HTTP requests for retrieving model outputs

## How It Works

This application extends the original Android-ChatBot project by incorporating the Octopus model for more advanced language processing capabilities. The app sends user input to the Octopus model via HTTP requests and displays the generated responses in the chat interface.

## Acknowledgements

This project is based on the [Android-ChatBot](https://github.com/gangulwar/Android-ChatBot) repository by gangulwar. We extend our gratitude for the initial codebase that made this project possible.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
45 changes: 45 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
id 'com.android.application'
}

android {
namespace 'com.nexa4ai.octopustest'
compileSdk 33

defaultConfig {
applicationId "com.nexa4ai.octopustest"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
71 changes: 71 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<!-- <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />-->
<!-- <uses-permission android:name="android.permission.WRITE_SETTINGS"/>-->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/octopus"
android:label="OctopusTalk"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/octopus_round"
android:supportsRtl="true"
android:theme="@style/Theme.OctopusTest"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
<queries>
<intent>
<action android:name="android.intent.action.SET_ALARM" />
</intent>
</queries>

</manifest>
118 changes: 118 additions & 0 deletions app/src/main/java/com/nexa4ai/octopustest/ApiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.nexa4ai.octopustest;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class ApiClient {
private static final String TAG = "ChatApp";
private static final String MODEL_ENDPOINT = "https://api.nexa4ai.com/android";
private final OkHttpClient client;
private final Context context;
private final MessageHandler messageHandler;

public ApiClient(Context context, MessageHandler messageHandler) {
this.client = new OkHttpClient();
this.context = context;
this.messageHandler = messageHandler;
}

public void sendMessage(String userMsg, ApiResponseCallback callback) {
Log.d(TAG, "sendMessage: Start - " + userMsg);
messageHandler.addMessage(new MessageModal(userMsg, "user"));

JsonObject payload = new JsonObject();
payload.addProperty("input_text", userMsg);

RequestBody body = RequestBody.create(payload.toString(), MediaType.parse("application/json"));
Request request = new Request.Builder()
.url(MODEL_ENDPOINT)
.post(body)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "sendMessage: API call failed", e);
((MainActivity) context).runOnUiThread(() -> callback.onFailure("API call failed"));
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
Log.e(TAG, "sendMessage: onResponse failed - " + response);
((MainActivity) context).runOnUiThread(() -> callback.onFailure("Response unsuccessful"));
return;
}

try (ResponseBody responseBody = response.body()) {
if (responseBody != null) {
String jsonResponse = responseBody.string();

// Log the full JSON string
Log.d(TAG, "Full JSON Response: " + jsonResponse);

JsonObject jsonObject = new Gson().fromJson(jsonResponse, JsonObject.class);
String botMessage = "Function: " + jsonObject.get("function_name").getAsString() +
"\nResult: " + jsonObject.get("result").toString() +
"\nLatency:" + jsonObject.get("latency").toString() + " s";
String functionName = jsonObject.get("function_name").getAsString();
Log.d(TAG, "functionName: " + functionName);
Type listType = new TypeToken<List<String>>() {
}.getType();

// Convert JsonArray to List<String>, assuming JSON is correctly formatted
List<String> functionArguments = new Gson().fromJson(jsonObject.get("function_arguments"),
listType);
((MainActivity) context)
.runOnUiThread(() -> callback.onSuccess(functionName, functionArguments));

String argumentsAsString = TextUtils.join(", ", functionArguments);
Log.d(TAG, "Function Arguments: " + argumentsAsString);

// Use the botMessage within runOnUiThread
((MainActivity) context).runOnUiThread(() -> {
messageHandler.addMessage(new MessageModal(botMessage, "bot"));
});
} else {
Log.e(TAG, "handleResponseSuccess: Response body is null");
handleResponseFailure();
}
}
}
});
Log.d(TAG, "sendMessage: End");
}

private void handleResponseFailure() {
Log.e(TAG, "handleResponseFailure: Handling failure");
((MainActivity) context).runOnUiThread(() -> {
Toast.makeText(context, "No response from the bot.", Toast.LENGTH_SHORT).show();
messageHandler.addMessage(new MessageModal("Sorry, no response found", "bot"));
});
}

public interface ApiResponseCallback {
void onSuccess(String functionName, List<String> functionArguments);

void onFailure(String message);
}
}
Loading

0 comments on commit a9fd805

Please sign in to comment.