How to easily create your AI (ChatGPT) based virtual voice assistant Android App?

Опубликовано: 08 Сентябрь 2023
на канале: Programmer World
4,521
101

In this video it shows the steps to build your own AI (ChatGPT) based voice assistant Android App.


In this demo it refers the code from programmer world's below 3 pages:
https://programmerworld.co/android/ho...
https://programmerworld.co/android/ho...
https://programmerworld.co/android/ho...

Some of the questions prompted for AI/ ChatGPT tool demo were:
- Who won the last football World Cup?
- How do you define God?
- Which is the best place to visit in Europe?

I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: [email protected]


Complete source code and other details/ steps of this video are posted in the below link:
https://programmerworld.co/android/ho...

However, the main Java code is copied below also for reference:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
textToSpeech.setSpeechRate((float) 0.8);
}
});
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);

speechRecognizer.setRecognitionListener(new RecognitionListener() {
public void onResults(Bundle bundle) {
ArrayList String matches = bundle.getStringArrayList(speechRecognizer.RESULTS_RECOGNITION);
String string = "";
textView.setText("");
if (matches != null) {
string = matches.get(0);
editText.setText(string);
chatGPTModel(string);
}
}
});
}

private void chatGPTModel(String stringInput){
textView.setText("In Progress ...");
textToSpeech.speak("In Progress", TextToSpeech.QUEUE_FLUSH, null,null);

JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("model", "gpt-3.5-turbo");

jsonObject.put("messages", jsonArrayMessage);

} catch (JSONException e) {
throw new RuntimeException(e);
}

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
stringURLEndPoint, jsonObject, new Response.Listener JSONObject () {
@Override
public void onResponse(JSONObject response) {

String stringText = null;
try {
stringText = response.getJSONArray("choices")
.getJSONObject(0)
.getJSONObject("message")
.getString("content");
} catch (JSONException e) {
throw new RuntimeException(e);
}
stringOutput = stringOutput + stringText;
textView.setText(stringOutput);
textToSpeech.speak(stringOutput, TextToSpeech.QUEUE_FLUSH, null,null);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}){
@Override
public Map String, String getHeaders() throws AuthFailureError {
Map String, String mapHeader = new HashMap ();
mapHeader.put("Authorization", "Bearer " + stringAPIKey);
mapHeader.put("Content-Type", "application/json");

return mapHeader;
}

@Override
protected Response JSONObject parseNetworkResponse(NetworkResponse response) {
return super.parseNetworkResponse(response);
}
};

int intTimeoutPeriod = 60000; // 60 seconds timeout duration defined
RetryPolicy retryPolicy = new DefaultRetryPolicy(intTimeoutPeriod,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonObjectRequest.setRetryPolicy(retryPolicy);
Volley.newRequestQueue(getApplicationContext()).add(jsonObjectRequest);
}
}


--