Commit 33bd5359 authored by Mahima Induvara's avatar Mahima Induvara

Chat Bot Stage 1 Complete

parent 57e6b6c3
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitSharedSettings">
<option name="FORCE_PUSH_PROHIBITED_PATTERNS">
<list />
</option>
</component>
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
plugins {
id 'com.android.application'
id 'com.chaquo.python'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
apply plugin: 'com.android.application'
apply plugin: 'com.chaquo.python'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
......@@ -60,6 +65,7 @@ dependencies {
implementation 'androidx.navigation:navigation-ui:2.3.5'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.archemistrylab">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......@@ -9,6 +11,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ARChemistryLab">
<activity android:name=".ChatbotMain"></activity>
<activity android:name="chatbot.MainActivity" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
......
package chatbot
import retrofit2.Call
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST
interface APIService {
@FormUrlEncoded
@POST("chat")
fun chatWithTheBit(@Field("chatInput") chatText : String ): Call<ChatResponse>
}
data class ChatResponse(val chatBotReply: String)
\ No newline at end of file
package chatbot
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.archemistrylab.R
import kotlinx.android.synthetic.main.listitem_chat.view.*
class AdapterChatBot : RecyclerView.Adapter<AdapterChatBot.MyViewHolder>() {
private val list = ArrayList<ChatModel>()
inner class MyViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.listitem_chat, parent, false)
) {
fun bind(chat: ChatModel) = with(itemView) {
if(!chat.isBot) {
txtChat.setBackgroundColor(Color.WHITE)
txtChat.setTextColor(Color.BLACK)
txtChat.text = chat.chat
}else{
txtChat.setBackgroundColor(Color.CYAN)
txtChat.setTextColor(Color.BLACK)
txtChat.text = chat.chat
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MyViewHolder(parent)
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(list[position])
}
override fun getItemCount() = list.size
fun addChatToList(chat: ChatModel) {
list.add(chat)
notifyDataSetChanged()
}
}
\ No newline at end of file
package chatbot
data class ChatModel(val chat: String, val isBot: Boolean = false) {
}
\ No newline at end of file
package chatbot
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.archemistrylab.R
import kotlinx.android.synthetic.main.activity_chatbot_main.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
private val adapterChatBot = AdapterChatBot()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chatbot_main)
val retrofit = Retrofit.Builder()
.baseUrl("http://192.168.1.100:5000")
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService = retrofit.create(APIService::class.java)
rvChatList.layoutManager = LinearLayoutManager(this)
rvChatList.adapter = adapterChatBot
btnSend.setOnClickListener {
if(etChat.text.isNullOrEmpty()){
Toast.makeText(this@MainActivity, "Please enter a text", Toast.LENGTH_LONG).show()
return@setOnClickListener
}
adapterChatBot.addChatToList(ChatModel(etChat.text.toString()))
apiService.chatWithTheBit(etChat.text.toString()).enqueue(callBack)
etChat.text.clear()
}
}
private val callBack = object : Callback<ChatResponse>{
override fun onResponse(call: Call<ChatResponse>, response: Response<ChatResponse>) {
if(response.isSuccessful && response.body()!= null){
adapterChatBot.addChatToList(ChatModel(response.body()!!.chatBotReply, true))
}else{
Toast.makeText(this@MainActivity, "NO!", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<ChatResponse>, t: Throwable) {
Toast.makeText(this@MainActivity, "Something went wrong", Toast.LENGTH_LONG).show()
}
}
}
\ No newline at end of file
package com.example.archemistrylab;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class ChatbotMain extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatbot_main);
}
}
\ No newline at end of file
package com.example.archemistrylab;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
......@@ -34,8 +35,7 @@ public class MainActivity extends AppCompatActivity {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startActivity(new Intent(MainActivity.this, chatbot.MainActivity.class));
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="chatbot.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvChatList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/etChat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/etChat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Chat with bot"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnSend"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Send"
app:layout_constraintBottom_toBottomOf="@+id/etChat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/etChat" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -30,39 +30,6 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvChatList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/etChat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/etChat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:ems="10"
android:hint="Chat with bot"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnSend"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Send"
app:layout_constraintBottom_toBottomOf="@+id/etChat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/etChat" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:padding="8dp"
android:id="@+id/txtChat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="" />
</LinearLayout>
\ No newline at end of file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext {
kotlin_version = '1.3.72'
}
repositories {
google()
jcenter()
......@@ -8,6 +11,7 @@ buildscript {
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
classpath "com.chaquo.python:gradle:9.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment