Tuesday 17 January 2023

How to Integrate ChatGPT In Android With TTS (Text-To-Speech).

AI (Artificial Intelligence) Technology has been progressing towards futuristic ideas on daily basis. OpenAI coming up with their R&D as ChatGPT is prominent development in this field. ChatGPT (Generative Pre-trained Transformer) is a chatbot launched by OpenAI in November 2022. It is built on top of OpenAI's GPT-3 family of large language models, and is fine-tuned with both supervised and reinforcement learning techniques.

Methods-
ChatGPT trained this model using Reinforcement Learning from Human Feedback (RLHF), using the same methods as InstructGPT, but with slight differences in the data collection setup. They trained an initial model using supervised fine-tuning: human AI trainers provided conversations in which they played both sides—the user and an AI assistant. Then provide the trainers access to model-written suggestions to help them compose their responses. They mixed this new dialogue dataset with the InstructGPT dataset, which are transformed into a dialogue format.


ChatGPT is fine-tuned from a model in the GPT-3.5 series, which finished training in early 2022. You can learn more about the 3.5 series here. ChatGPT and GPT 3.5 were trained on an Azure AI supercomputing infrastructure.

We as technophiles strongly believe in ‘Sharing Is Caring’. We have create a sample demo where we will integrate ChatGPT in Android to demonstrate how it works with TTS (Text-To-Speech).


Here, we have asked for food recipe via ChatGPT bot which is read by TTS bot. We can also create a recipe from a list of ingredients. There are unlimited use-cases and ideas that are possible to explore and make life easy and technological advance.

Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//MainActivity.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.elitetechnority.chatgptdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.StrictMode;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextToSpeech textToSpeech;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OpenAiService service = new OpenAiService("Your Token");
        TextView tv_bot=(TextView)findViewById(R.id.tv_bot);
        EditText et_bot=(EditText)findViewById(R.id.et_bot);
        et_bot.setText("Tell about ChatGPT");
        Button btn_bot=(Button)findViewById(R.id.btn_bot);
        btn_bot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CompletionRequest completionRequest = CompletionRequest.builder()
                        .prompt(et_bot.getText().toString())
                        .model("text-davinci-003")
                        .topP(1.0)
                        .temperature(0.3)
                        .maxTokens(150)
                        .frequencyPenalty(0.0)
                        .presencePenalty(0.0)
                        .build();
                CompletionResult completionResult=service.createCompletion(completionRequest);
                System.out.println(completionResult);
                tv_bot.setText(completionResult.getChoices().get(0).getText());
                String toSpeak = tv_bot.getText().toString();
                textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
            }
        });

         textToSpeech=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if(status != TextToSpeech.ERROR) {
                    textToSpeech.setLanguage(Locale.UK);
                }
            }
        });
    }

    @Override
    protected void onPause() {
        if(textToSpeech !=null){
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        if(textToSpeech !=null){
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
        super.onDestroy();
    }
}

No comments:

Post a Comment