Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

05 speech recognition #1

Open
wants to merge 5 commits into
base: state_main_fragment
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.theokanning.openai-gpt3-java:service:0.14.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {

public static final ExecutorService backgroundExecutorService = Executors.newFixedThreadPool(4);
public static final Handler uiThreadHandler = new Handler(Looper.getMainLooper());

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,40 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;

import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.Locale;

import de.fhdw.app_entwicklung.chatgpt.openai.ChatGpt;
import de.fhdw.app_entwicklung.chatgpt.speech.LauchTextToSpeech;
import de.fhdw.app_entwicklung.chatgpt.speech.LaunchSpeechRecognition;

public class MainFragment extends Fragment {
private final ActivityResultLauncher<LaunchSpeechRecognition.SpeechRecognitionArgs> getTextFromSpeech = registerForActivityResult(
new LaunchSpeechRecognition(),
query -> {
getTextView().append(query);

LauchTextToSpeech ttp = new LauchTextToSpeech(this.getContext());

MainActivity.backgroundExecutorService.execute(() ->
{
ChatGpt chatGpt = new ChatGpt("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");
String answer = chatGpt.getChatCompletion(query);
getTextView().setText(answer);
ttp.Speak(answer);

});


});

public MainFragment() {
}
Expand All @@ -18,4 +48,25 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

getAskButton().setOnClickListener(v -> MainActivity.backgroundExecutorService.execute(() ->
{
getTextFromSpeech.launch(new LaunchSpeechRecognition.SpeechRecognitionArgs());

}));
}

private TextView getTextView() {
//noinspection ConstantConditions
return getView().findViewById(R.id.textView);
}

private Button getAskButton() {
//noinspection ConstantConditions
return getView().findViewById(R.id.button_ask);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.fhdw.app_entwicklung.chatgpt.openai;

import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatCompletionResult;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import com.theokanning.openai.service.OpenAiService;

import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;

public class ChatGpt {

private final String apiToken;

public ChatGpt(String apiToken) {
this.apiToken = apiToken;
}

public String getChatCompletion(String query) {
OpenAiService service = new OpenAiService(apiToken, Duration.ofSeconds(90));

try {
ChatMessage message = new ChatMessage(ChatMessageRole.USER.value(), query);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo")
.messages(Collections.singletonList(message))
.n(1)
.maxTokens(2048)
.logitBias(new HashMap<>())
.build();

ChatCompletionResult result = service.createChatCompletion(chatCompletionRequest);
if (result.getChoices().size() != 1) {
throw new RuntimeException("Received unexpected number of chat completions: should be 1, but received " + result.getChoices().size());
}

return result.getChoices().get(0).getMessage().getContent();
}
finally {
service.shutdownExecutor();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.fhdw.app_entwicklung.chatgpt.speech;

import android.speech.tts.TextToSpeech;
import android.content.Context;

public class LauchTextToSpeech implements TextToSpeech.OnInitListener{
private TextToSpeech ttp;
public LauchTextToSpeech(Context context){
ttp = new TextToSpeech(context, this);
}

@Override
public void onInit(int status){

}

public void Speak(String text){
ttp.speak(text, 0, null ,null);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.fhdw.app_entwicklung.chatgpt.speech;

import android.content.Context;
import android.content.Intent;
import android.speech.RecognitionPart;
import android.speech.RecognizerIntent;
import androidx.activity.result.contract.ActivityResultContract;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Locale;
import java.util.Objects;

import java.util.Locale;

public class LaunchSpeechRecognition extends ActivityResultContract<LaunchSpeechRecognition.SpeechRecognitionArgs, String> {

@NonNull
@Override
public Intent createIntent(@NonNull Context context, SpeechRecognitionArgs speechRecognitionArgs) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
//intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Hello");
return intent;
}

@Override
public String parseResult(int resultCode, @Nullable Intent data) {
if (resultCode == android.app.Activity.RESULT_OK && data != null) {
ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (results != null && !results.isEmpty()) {
return Objects.requireNonNull(results).get(0);
}
}
return null;
}

public static class SpeechRecognitionArgs
{
public SpeechRecognitionArgs()
{
//this.locale = locale;
}

}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
android:layout_height="0dp"
android:fillViewport="true"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintBottom_toTopOf="@+id/button_ask"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/button"
android:id="@+id/button_ask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:text="@string/ask"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">ChatGpt</string>
<string name="ask">Ask</string>
</resources>