Zainajabroh commited on
Commit
038ca43
·
verified ·
1 Parent(s): 5e4c4a4

Update Discussion_Buddy.py

Browse files
Files changed (1) hide show
  1. Discussion_Buddy.py +264 -264
Discussion_Buddy.py CHANGED
@@ -1,264 +1,264 @@
1
- # Install all of it
2
- #!pip install python-dotenv
3
- #!pip install -q -U google-generativeai
4
- #!pip install -U langchain-community
5
- #!pip install faiss-cpu
6
- #!pip install langchain_google_genai
7
- #!pip install gradio
8
- # import all library, framework and load all model we need
9
- from joblib import load
10
- import gradio as gr
11
- import re
12
- import nltk
13
- from nltk.tokenize.toktok import ToktokTokenizer
14
- from nltk.stem import LancasterStemmer
15
- from nltk.corpus import stopwords
16
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
17
- import torch
18
- import torch.nn as nn
19
- import cv2
20
- import numpy as np
21
- from torchvision import models,transforms
22
- import pickle
23
- import os
24
- from dotenv import load_dotenv
25
- import google.generativeai as genai
26
- from langchain.text_splitter import RecursiveCharacterTextSplitter
27
- from langchain_google_genai import GoogleGenerativeAIEmbeddings,ChatGoogleGenerativeAI
28
- from langchain_community.vectorstores import FAISS
29
- from langchain.chains import RetrievalQA
30
- from langchain_google_genai import GoogleGenerativeAI
31
- from langchain.chains import ConversationChain
32
- from langchain.schema import HumanMessage, AIMessage
33
- from langchain.memory import ConversationBufferMemory
34
- nltk.download('stopwords')
35
- # load all model and API
36
- device = 0 if torch.cuda.is_available() else -1 # check if GPU available using GPU
37
- translator_eng_id = pipeline('translation', model='Helsinki-NLP/opus-mt-en-id', device = device)# eng-indo
38
- translator_id_eng = pipeline('translation', model='Helsinki-NLP/opus-mt-id-en', device = device)# indo-eng
39
- summarizer = pipeline('summarization', model='facebook/bart-large-cnn', device = device) # summarize the news
40
- ner = pipeline('ner',tokenizer = 'dbmdz/bert-large-cased-finetuned-conll03-english', model='dbmdz/bert-large-cased-finetuned-conll03-english', device = device) # Analyze NER on news
41
- lang_detector = pipeline('text-classification', model = 'papluca/xlm-roberta-base-language-detection', device = device) #detect language
42
- news_encoder = load('news_encoder.pkl') #encoder for the news (3 class)
43
- news_classifier = load("news_classifier.pkl") #classify news into 3 category (Politic, Technology, Science)
44
- emotion_text_encoder = load("emotion_encoder.pkl") # Encoder for Emotion detection(text version)
45
- emotion_classifier = load("emotion_model_classifier.pkl") #Detect emotion user based on their comment about news
46
- with open('class_names_face_recognition.pkl', 'rb') as f: # import and using it as encoder for emotion detection(image version)
47
- emotion_face_class = pickle.load(f)
48
- face_emotion = models.resnet18(pretrained=False) # Detect user emotion based on their expression (selfie)
49
- face_emotion.fc = nn.Linear(face_emotion.fc.in_features, 5)
50
- #face_emotion = face_emotion.load_state_dict(torch.load('Best_emotion_face_model.pth'),map_location=torch.device('cpu'))
51
- device = torch.device("cuda" if torch.cuda.is_available() else 'cpu') # move into GPU if available, so model will using GPU
52
- state_dict = torch.load('Best_emotion_face_model.pth', map_location=device)
53
- face_emotion.load_state_dict(state_dict)
54
- face_emotion.to(device)
55
- face_emotion.eval()
56
- # using API Gemini for chatbot
57
- GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY")
58
- genai.configure(api_key=GOOGLE_API_KEY)
59
-
60
- # Support Function
61
- #detect language
62
- def detect_language(text):
63
- result = lang_detector(text)
64
- return result[0]['label']
65
- #translate to english if user language not english
66
- def translate_to_english(text,source_lang):
67
- if source_lang.lower() == 'indonesia':
68
- return translator_id_eng(text)[0]['translation_text']
69
- else:
70
- return text
71
- def translate_to_original(text,target_lang):
72
- if target_lang.lower() == 'indonesia':
73
- return translator_eng_id(text)[0]['translation_text']
74
- else:
75
- return text
76
- # upload reaction
77
- def process_image_reaction(image_path):
78
- image = cv2.imread(image_path)
79
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
80
- image = image.astype(np.float32)/255.0
81
- image = np.transpose(image, (2,0,1))
82
- image = torch.tensor(image).unsqueeze(0).to(device)
83
- #predict
84
- with torch.no_grad():
85
- outputs = face_emotion(image)
86
- _,predicted = torch.max(outputs,1)
87
- return emotion_face_class[predicted.item()]
88
- #make template for cleaning
89
- def cleaning_text(text, is_lower_case = False):
90
- text = text.lower()
91
- pattern = r'[^a-zA-z0-9\s]'
92
- text = re.sub(pattern,'',text)
93
- tokenizer = ToktokTokenizer()
94
- stopword = stopwords.words('english')
95
- tokens = tokenizer.tokenize(text)
96
- tokens = [token.strip() for token in tokens]
97
- if is_lower_case:
98
- filtered_tokens = [token for token in tokens if token not in stopword]
99
- else:
100
- filtered_tokens = [token for token in tokens if token.lower() not in stopword]
101
- filtered_text =' '.join(filtered_tokens)
102
- st = LancasterStemmer()
103
- text = ' '.join([st.stem(word) for word in filtered_text.split()])
104
- return text
105
-
106
- #Step 1 to classify, summary and get NER from news
107
- def extract_article(url):
108
- model = genai.GenerativeModel('gemini-1.5-pro')
109
- extract = model.generate_content(f"Extract the content from this news article: {url}, make sure extract all news part. Extract based on the news language used, if the news using Indonesia language just extract with Indonesian language, and so on if the news using english language. Make sure minimum is 300 words and maximum is 400 words").text
110
- return extract
111
- def process_news_pipeline(news_text, user_language):
112
- news_text = extract_article(news_text)
113
- #detect the language
114
- detected_lang = detect_language(news_text)
115
- #translate news if not in english
116
- if detected_lang.lower() != 'english':
117
- news_text = translate_to_english(news_text,detected_lang)
118
- #analyze the news
119
- #classify the news category
120
- summary = summarizer(news_text, min_length = 25, do_sample = False)[0]['summary_text']
121
- clean_text = cleaning_text(summary)
122
- classification = news_classifier.predict([clean_text])[0]
123
- classification = news_encoder.inverse_transform([classification])[0]
124
- ner_result = ner(news_text)
125
- ner_filter = [{"entity":result['entity'],'word':result['word']} for result in ner_result]
126
- ner_filter = ner_filter[:5]
127
- #translate result into original language if not english
128
- if user_language.lower() != 'english':
129
- classification = translate_to_original(classification, user_language)
130
- summary = translate_to_original(summary, user_language)
131
- return classification, summary, ner_filter
132
- def bring_together_all_of_it(news_text, user_language):
133
- news_text = extract_article(news_text)
134
- classification, summary, ner_results = process_news_pipeline(news_text,user_language)
135
- ner_results = "\n".join([f"Entity: {item['entity']}, Word: {item['word']}" for item in ner_results])
136
- return classification, summary, ner_results, news_text
137
-
138
- #Step 2 for predict user emotion based on text or image
139
- def process_user_response(response_type, response_content, user_language):
140
- if response_type == "Text" and response_content:
141
- sentiment = emotion_text_encoder.inverse_transform([emotion_classifier.predict([response_content])[0]])[0] #predict output using sentiment analysis model
142
- if user_language != 'English':
143
- translate = translate_to_english(response_content,user_language)
144
- sentiment = emotion_text_encoder.inverse_transform([emotion_classifier.predict([translate])[0]])[0] #predict output using sentiment analysis model
145
- sentiment = translate_to_original(sentiment,user_language)
146
- elif response_type == "Selfie" and response_content:
147
- sentiment = process_image_reaction(response_content) # predict output using image classification model
148
- if user_language != 'English':
149
- sentiment = translate_to_original(sentiment,user_language)
150
- else:
151
- return "Please provide valid input."
152
- return f"Your Expression: {sentiment}"
153
-
154
- #Step 3, make chatbot for discussion about the news
155
- #load model and memory, to save context while discuss with chatbot
156
- model = GoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.1)
157
- memory = ConversationBufferMemory()
158
- conversation = ConversationChain(
159
- llm=model,
160
- verbose=False,
161
- memory=memory
162
- )
163
- def chatbot_interaction(user_message, history, news, user_reaction, user_language):
164
- logo = "https://i.ibb.co.com/rwft0JQ/output-removebg-preview-1.png"
165
- if history is None:
166
- history = []
167
- # first response based on bot response to know the context and save it into history
168
- if len(history) == 0:
169
- initial_response = f"Hi genius! wanna discuss something?"
170
- memory.chat_memory.add_ai_message(initial_response)
171
- memory.chat_memory.add_user_message(f"Learn and become good presenter based on this news {news}, after that you will discuss with user about this news and make sure using {user_language} language and their ekspression : {user_reaction}")
172
- history.append([f"👤 {user_message}", f"<img src='{logo}' style='width:30px; height:30px;'> { initial_response}"])
173
- return history, ""
174
- memory.chat_memory.add_user_message(user_message)
175
- gpt_response = conversation.predict(input=user_message)
176
- memory.chat_memory.add_ai_message(gpt_response)
177
- history.append([f"👤 {user_message}",f"<img src='{logo}' style='width:30px; height:30px;'> {gpt_response}"] )
178
- return history, ""
179
-
180
- #Building UI from Gradio
181
- #Step 1
182
- with gr.Blocks() as news_analysis_interface:
183
- news_text = gr.Textbox(label="Put the news link")
184
- user_language = gr.Radio(choices=["English", "Indonesia"], label="User Language")
185
- submit_analysis = gr.Button("Analyze News")
186
- summary_result = gr.Textbox(label="News Classification", elem_id="summary_result_box")
187
- classification_result = gr.Textbox(label="News Summary")
188
- ner_result = gr.Textbox(label="Named Entities")
189
- news_state = gr.Textbox(label="News")
190
- submit_analysis.click(
191
- fn=bring_together_all_of_it,
192
- inputs=[news_text, user_language],
193
- outputs=[summary_result, classification_result, ner_result, news_state]
194
- )
195
-
196
- #step 2
197
- with gr.Blocks() as reaction_interface:
198
- response_type = gr.Radio(["Text", "Selfie"], label="Response Type")
199
- text_input = gr.Textbox(label="Enter Text (only understand english comment)", visible=False)
200
- image_input = gr.Image(type="filepath", label="Upload Image", visible=False)
201
- reaction_result = gr.Textbox(label="Detected Reaction", interactive=False)
202
- def update_visibility(selected_response):
203
- if selected_response == "Text":
204
- return gr.update(visible=True), gr.update(visible=False)
205
- elif selected_response == "Selfie":
206
- return gr.update(visible=False), gr.update(visible=True)
207
- else:
208
- return gr.update(visible=False), gr.update(visible=False)
209
- response_type.change(
210
- fn=update_visibility,
211
- inputs=response_type,
212
- outputs=[text_input, image_input]
213
- )
214
- submit_button = gr.Button("Submit Reaction")
215
- def submit_click(response_type_value, text_value, image_value,language_value):
216
- if response_type_value == "Text" and text_value:
217
- processed_reaction = process_user_response(response_type_value, text_value,language_value)
218
- return processed_reaction
219
- elif response_type_value == "Selfie" and image_value:
220
- processed_reaction = process_user_response(response_type_value, image_value,language_value)
221
- return processed_reaction
222
- else:
223
- return "Please provide valid input."
224
- submit_button.click(
225
- fn = submit_click,
226
- inputs=[response_type, text_input, image_input, user_language],
227
- outputs=[reaction_result]
228
- )
229
-
230
- #step 3
231
- with gr.Blocks() as chatbot_interface:
232
- chatbot_chatbox = gr.Chatbot(label="Chatbot Interaction")
233
- chatbot_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
234
- chatbot_submit_button = gr.Button("Send")
235
- chatbot_history = gr.State(value=[])
236
- chatbot_submit_button.click(
237
- fn=chatbot_interaction,
238
- inputs=[chatbot_input,chatbot_history,news_state,reaction_result,user_language],
239
- outputs=[chatbot_chatbox,chatbot_input]
240
- )
241
- chatbot_input.submit(fn=chatbot_interaction,
242
- inputs=[chatbot_input,chatbot_history,news_state,reaction_result,user_language],
243
- outputs=[chatbot_chatbox,chatbot_input]
244
- )
245
-
246
- #Bring it all and launch
247
- with gr.Blocks(theme=gr.themes.Default(primary_hue='gray', secondary_hue='zinc')) as full_app:
248
- with gr.Column():
249
- # Membuat gambar terpusat di atas
250
- gr.HTML("""
251
- <div style="text-align: center;">
252
- <img src="https://i.ibb.co.com/rwft0JQ/output-removebg-preview-1.png" alt="Robot Logo" style="width:150px; display: block; margin: 0 auto;">
253
- </div>
254
- """)
255
- # Menambahkan judul
256
- gr.Markdown("<h1 style='text-align: center;'>Your Discussion Friend</h1>")
257
- #gr.Markdown("# Your Discussion Friend")
258
- gr.Markdown("**Step 1**: Analyze the News")
259
- news_analysis_interface.render()
260
- gr.Markdown("**Step 2**: Choose Your Reaction")
261
- reaction_interface.render()
262
- gr.Markdown("**Step 3**: Interact with Chatbot")
263
- chatbot_interface.render()
264
- full_app.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ # Install all of it
2
+ #!pip install python-dotenv
3
+ #!pip install -q -U google-generativeai
4
+ #!pip install -U langchain-community
5
+ #!pip install faiss-cpu
6
+ #!pip install langchain_google_genai
7
+ #!pip install gradio
8
+ # import all library, framework and load all model we need
9
+ from joblib import load
10
+ import gradio as gr
11
+ import re
12
+ import nltk
13
+ from nltk.tokenize.toktok import ToktokTokenizer
14
+ from nltk.stem import LancasterStemmer
15
+ from nltk.corpus import stopwords
16
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
17
+ import torch
18
+ import torch.nn as nn
19
+ import cv2
20
+ import numpy as np
21
+ from torchvision import models,transforms
22
+ import pickle
23
+ import os
24
+ from dotenv import load_dotenv
25
+ import google.generativeai as genai
26
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
27
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings,ChatGoogleGenerativeAI
28
+ from langchain_community.vectorstores import FAISS
29
+ from langchain.chains import RetrievalQA
30
+ from langchain_google_genai import GoogleGenerativeAI
31
+ from langchain.chains import ConversationChain
32
+ from langchain.schema import HumanMessage, AIMessage
33
+ from langchain.memory import ConversationBufferMemory
34
+ nltk.download('stopwords')
35
+ # load all model and API
36
+ device = 0 if torch.cuda.is_available() else -1 # check if GPU available using GPU
37
+ translator_eng_id = pipeline('translation', model='Helsinki-NLP/opus-mt-en-id', device = device)# eng-indo
38
+ translator_id_eng = pipeline('translation', model='Helsinki-NLP/opus-mt-id-en', device = device)# indo-eng
39
+ summarizer = pipeline('summarization', model='facebook/bart-large-cnn', device = device) # summarize the news
40
+ ner = pipeline('ner',tokenizer = 'dbmdz/bert-large-cased-finetuned-conll03-english', model='dbmdz/bert-large-cased-finetuned-conll03-english', device = device) # Analyze NER on news
41
+ lang_detector = pipeline('text-classification', model = 'papluca/xlm-roberta-base-language-detection', device = device) #detect language
42
+ news_encoder = load('news_encoder.pkl') #encoder for the news (3 class)
43
+ news_classifier = load("news_classifier.pkl") #classify news into 3 category (Politic, Technology, Science)
44
+ emotion_text_encoder = load("emotion_encoder.pkl") # Encoder for Emotion detection(text version)
45
+ emotion_classifier = load("emotion_model_classifier.pkl") #Detect emotion user based on their comment about news
46
+ with open('class_names_face_recognition.pkl', 'rb') as f: # import and using it as encoder for emotion detection(image version)
47
+ emotion_face_class = pickle.load(f)
48
+ face_emotion = models.resnet18(pretrained=False) # Detect user emotion based on their expression (selfie)
49
+ face_emotion.fc = nn.Linear(face_emotion.fc.in_features, 5)
50
+ #face_emotion = face_emotion.load_state_dict(torch.load('Best_emotion_face_model.pth'),map_location=torch.device('cpu'))
51
+ device = torch.device("cuda" if torch.cuda.is_available() else 'cpu') # move into GPU if available, so model will using GPU
52
+ state_dict = torch.load('Best_emotion_face_model.pth', map_location=device)
53
+ face_emotion.load_state_dict(state_dict)
54
+ face_emotion.to(device)
55
+ face_emotion.eval()
56
+ # using API Gemini for chatbot
57
+ GOOGLE_API_KEY = os.getenv("GEMINI_API_KEY")
58
+ genai.configure(api_key=GOOGLE_API_KEY)
59
+
60
+ # Support Function
61
+ #detect language
62
+ def detect_language(text):
63
+ result = lang_detector(text)
64
+ return result[0]['label']
65
+ #translate to english if user language not english
66
+ def translate_to_english(text,source_lang):
67
+ if source_lang.lower() == 'indonesia':
68
+ return translator_id_eng(text)[0]['translation_text']
69
+ else:
70
+ return text
71
+ def translate_to_original(text,target_lang):
72
+ if target_lang.lower() == 'indonesia':
73
+ return translator_eng_id(text)[0]['translation_text']
74
+ else:
75
+ return text
76
+ # upload reaction
77
+ def process_image_reaction(image_path):
78
+ image = cv2.imread(image_path)
79
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
80
+ image = image.astype(np.float32)/255.0
81
+ image = np.transpose(image, (2,0,1))
82
+ image = torch.tensor(image).unsqueeze(0).to(device)
83
+ #predict
84
+ with torch.no_grad():
85
+ outputs = face_emotion(image)
86
+ _,predicted = torch.max(outputs,1)
87
+ return emotion_face_class[predicted.item()]
88
+ #make template for cleaning
89
+ def cleaning_text(text, is_lower_case = False):
90
+ text = text.lower()
91
+ pattern = r'[^a-zA-z0-9\s]'
92
+ text = re.sub(pattern,'',text)
93
+ tokenizer = ToktokTokenizer()
94
+ stopword = stopwords.words('english')
95
+ tokens = tokenizer.tokenize(text)
96
+ tokens = [token.strip() for token in tokens]
97
+ if is_lower_case:
98
+ filtered_tokens = [token for token in tokens if token not in stopword]
99
+ else:
100
+ filtered_tokens = [token for token in tokens if token.lower() not in stopword]
101
+ filtered_text =' '.join(filtered_tokens)
102
+ st = LancasterStemmer()
103
+ text = ' '.join([st.stem(word) for word in filtered_text.split()])
104
+ return text
105
+
106
+ #Step 1 to classify, summary and get NER from news
107
+ def extract_article(url):
108
+ model = genai.GenerativeModel('gemini-1.5-pro')
109
+ extract = model.generate_content(f"Extract the content from this news article: {url}, make sure extract all news part. Extract based on the news language used, if the news using Indonesia language just extract with Indonesian language, and so on if the news using english language. Make sure minimum is 300 words and maximum is 400 words").text
110
+ return extract
111
+ def process_news_pipeline(news_text, user_language):
112
+ news_text = extract_article(news_text)
113
+ #detect the language
114
+ detected_lang = detect_language(news_text)
115
+ #translate news if not in english
116
+ if detected_lang.lower() != 'english':
117
+ news_text = translate_to_english(news_text,detected_lang)
118
+ #analyze the news
119
+ #classify the news category
120
+ summary = summarizer(news_text, min_length = 25, do_sample = False)[0]['summary_text']
121
+ clean_text = cleaning_text(summary)
122
+ classification = news_classifier.predict([clean_text])[0]
123
+ classification = news_encoder.inverse_transform([classification])[0]
124
+ ner_result = ner(news_text)
125
+ ner_filter = [{"entity":result['entity'],'word':result['word']} for result in ner_result]
126
+ ner_filter = ner_filter[:5]
127
+ #translate result into original language if not english
128
+ if user_language.lower() != 'english':
129
+ classification = translate_to_original(classification, user_language)
130
+ summary = translate_to_original(summary, user_language)
131
+ return classification, summary, ner_filter
132
+ def bring_together_all_of_it(news_text, user_language):
133
+ news_text = extract_article(news_text)
134
+ classification, summary, ner_results = process_news_pipeline(news_text,user_language)
135
+ ner_results = "\n".join([f"Entity: {item['entity']}, Word: {item['word']}" for item in ner_results])
136
+ return classification, summary, ner_results, news_text
137
+
138
+ #Step 2 for predict user emotion based on text or image
139
+ def process_user_response(response_type, response_content, user_language):
140
+ if response_type == "Text" and response_content:
141
+ sentiment = emotion_text_encoder.inverse_transform([emotion_classifier.predict([response_content])[0]])[0] #predict output using sentiment analysis model
142
+ if user_language != 'English':
143
+ translate = translate_to_english(response_content,user_language)
144
+ sentiment = emotion_text_encoder.inverse_transform([emotion_classifier.predict([translate])[0]])[0] #predict output using sentiment analysis model
145
+ sentiment = translate_to_original(sentiment,user_language)
146
+ elif response_type == "Selfie" and response_content:
147
+ sentiment = process_image_reaction(response_content) # predict output using image classification model
148
+ if user_language != 'English':
149
+ sentiment = translate_to_original(sentiment,user_language)
150
+ else:
151
+ return "Please provide valid input."
152
+ return f"Your Expression: {sentiment}"
153
+
154
+ #Step 3, make chatbot for discussion about the news
155
+ #load model and memory, to save context while discuss with chatbot
156
+ model = GoogleGenerativeAI(model="gemini-1.5-pro", temperature=0.1)
157
+ memory = ConversationBufferMemory()
158
+ conversation = ConversationChain(
159
+ llm=model,
160
+ verbose=False,
161
+ memory=memory
162
+ )
163
+ def chatbot_interaction(user_message, history, news, user_reaction, user_language):
164
+ logo = "https://i.ibb.co.com/rwft0JQ/output-removebg-preview-1.png"
165
+ if history is None:
166
+ history = []
167
+ # first response based on bot response to know the context and save it into history
168
+ if len(history) == 0:
169
+ initial_response = f"Hi genius! wanna discuss something?"
170
+ memory.chat_memory.add_ai_message(initial_response)
171
+ memory.chat_memory.add_user_message(f"Learn and become good presenter based on this news {news}, after that you will discuss with user about this news and make sure using {user_language} language and their ekspression : {user_reaction}")
172
+ history.append([f"👤 {user_message}", f"<img src='{logo}' style='width:30px; height:30px;'> { initial_response}"])
173
+ return history, ""
174
+ memory.chat_memory.add_user_message(user_message)
175
+ gpt_response = conversation.predict(input=user_message)
176
+ memory.chat_memory.add_ai_message(gpt_response)
177
+ history.append([f"👤 {user_message}",f"<img src='{logo}' style='width:30px; height:30px;'> {gpt_response}"] )
178
+ return history, ""
179
+
180
+ #Building UI from Gradio
181
+ #Step 1
182
+ with gr.Blocks() as news_analysis_interface:
183
+ news_text = gr.Textbox(label="Put the news link")
184
+ user_language = gr.Radio(choices=["English", "Indonesia"], label="User Language")
185
+ submit_analysis = gr.Button("Analyze News")
186
+ summary_result = gr.Textbox(label="News Classification", elem_id="summary_result_box")
187
+ classification_result = gr.Textbox(label="News Summary")
188
+ ner_result = gr.Textbox(label="Named Entities")
189
+ news_state = gr.Textbox(label="News")
190
+ submit_analysis.click(
191
+ fn=bring_together_all_of_it,
192
+ inputs=[news_text, user_language],
193
+ outputs=[summary_result, classification_result, ner_result, news_state]
194
+ )
195
+
196
+ #step 2
197
+ with gr.Blocks() as reaction_interface:
198
+ response_type = gr.Radio(["Text", "Selfie"], label="Response Type")
199
+ text_input = gr.Textbox(label="Enter Text (only understand english comment)", visible=False)
200
+ image_input = gr.Image(type="filepath", label="Upload Image", visible=False)
201
+ reaction_result = gr.Textbox(label="Detected Reaction", interactive=False)
202
+ def update_visibility(selected_response):
203
+ if selected_response == "Text":
204
+ return gr.update(visible=True), gr.update(visible=False)
205
+ elif selected_response == "Selfie":
206
+ return gr.update(visible=False), gr.update(visible=True)
207
+ else:
208
+ return gr.update(visible=False), gr.update(visible=False)
209
+ response_type.change(
210
+ fn=update_visibility,
211
+ inputs=response_type,
212
+ outputs=[text_input, image_input]
213
+ )
214
+ submit_button = gr.Button("Submit Reaction")
215
+ def submit_click(response_type_value, text_value, image_value,language_value):
216
+ if response_type_value == "Text" and text_value:
217
+ processed_reaction = process_user_response(response_type_value, text_value,language_value)
218
+ return processed_reaction
219
+ elif response_type_value == "Selfie" and image_value:
220
+ processed_reaction = process_user_response(response_type_value, image_value,language_value)
221
+ return processed_reaction
222
+ else:
223
+ return "Please provide valid input."
224
+ submit_button.click(
225
+ fn = submit_click,
226
+ inputs=[response_type, text_input, image_input, user_language],
227
+ outputs=[reaction_result]
228
+ )
229
+
230
+ #step 3
231
+ with gr.Blocks() as chatbot_interface:
232
+ chatbot_chatbox = gr.Chatbot(label="Chatbot Interaction")
233
+ chatbot_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
234
+ chatbot_submit_button = gr.Button("Send")
235
+ chatbot_history = gr.State(value=[])
236
+ chatbot_submit_button.click(
237
+ fn=chatbot_interaction,
238
+ inputs=[chatbot_input,chatbot_history,news_state,reaction_result,user_language],
239
+ outputs=[chatbot_chatbox,chatbot_input]
240
+ )
241
+ chatbot_input.submit(fn=chatbot_interaction,
242
+ inputs=[chatbot_input,chatbot_history,news_state,reaction_result,user_language],
243
+ outputs=[chatbot_chatbox,chatbot_input]
244
+ )
245
+
246
+ #Bring it all and launch
247
+ with gr.Blocks(theme=gr.themes.Default(primary_hue='gray', secondary_hue='zinc')) as full_app:
248
+ with gr.Column():
249
+ # Membuat gambar terpusat di atas
250
+ gr.HTML("""
251
+ <div style="text-align: center;">
252
+ <img src="https://i.ibb.co.com/rwft0JQ/output-removebg-preview-1.png" alt="Robot Logo" style="width:150px; display: block; margin: 0 auto;">
253
+ </div>
254
+ """)
255
+ # Menambahkan judul
256
+ gr.Markdown("<h1 style='text-align: center;'>Your Discussion Friend</h1>")
257
+ #gr.Markdown("# Your Discussion Friend")
258
+ gr.Markdown("**Step 1**: Analyze the News")
259
+ news_analysis_interface.render()
260
+ gr.Markdown("**Step 2**: Choose Your Reaction")
261
+ reaction_interface.render()
262
+ gr.Markdown("**Step 3**: Interact with Chatbot")
263
+ chatbot_interface.render()
264
+ full_app.launch()