Hokeno commited on
Commit
9050d5f
·
verified ·
1 Parent(s): 563246a

Upload basic_conv2.ipynb

Browse files
Files changed (1) hide show
  1. basic_conv2.ipynb +194 -0
basic_conv2.ipynb ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "142fd788",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stderr",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "C:\\Users\\BUDI\\AppData\\Local\\Temp\\ipykernel_24340\\2248876341.py:98: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.\n",
14
+ " chatbot = gr.Chatbot(label=\"Percakapan\", elem_classes=\"chatbot\", show_copy_button=True)\n"
15
+ ]
16
+ },
17
+ {
18
+ "name": "stdout",
19
+ "output_type": "stream",
20
+ "text": [
21
+ "* Running on local URL: http://127.0.0.1:7860\n",
22
+ "* To create a public link, set `share=True` in `launch()`.\n"
23
+ ]
24
+ },
25
+ {
26
+ "data": {
27
+ "text/html": [
28
+ "<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
29
+ ],
30
+ "text/plain": [
31
+ "<IPython.core.display.HTML object>"
32
+ ]
33
+ },
34
+ "metadata": {},
35
+ "output_type": "display_data"
36
+ }
37
+ ],
38
+ "source": [
39
+ "import gradio as gr\n",
40
+ "import logging\n",
41
+ "import os\n",
42
+ "from groq import Groq\n",
43
+ "from dotenv import load_dotenv\n",
44
+ "\n",
45
+ "load_dotenv()\n",
46
+ "\n",
47
+ "logging.basicConfig(level=logging.INFO, filename=\"chatbot_v10.log\", filemode=\"a\",\n",
48
+ " format=\"%(asctime)s - %(levelname)s - %(message)s\")\n",
49
+ "logger = logging.getLogger(__name__)\n",
50
+ "\n",
51
+ "client = Groq(api_key=os.getenv('GROQ_API_KEY'))\n",
52
+ "\n",
53
+ "def generate_response_groq(user_input: str, current_history: list) -> str:\n",
54
+ " \"\"\"\n",
55
+ " Menghasilkan respons dari Groq API berdasarkan input pengguna dan riwayat percakapan.\n",
56
+ " \n",
57
+ " Args:\n",
58
+ " user_input (str): Pesan yang dimasukkan oleh pengguna.\n",
59
+ " current_history (list): Riwayat percakapan Gradio, berupa list of tuples (user_msg, bot_msg).\n",
60
+ "\n",
61
+ " Returns:\n",
62
+ " str: Respons dari asisten AI.\n",
63
+ " \"\"\"\n",
64
+ " try:\n",
65
+ " messages_for_api = []\n",
66
+ " for user_msg, bot_msg in current_history:\n",
67
+ " messages_for_api.append({\"role\": \"user\", \"content\": user_msg})\n",
68
+ " messages_for_api.append({\"role\": \"assistant\", \"content\": bot_msg})\n",
69
+ " \n",
70
+ " messages_for_api.append({\"role\": \"user\", \"content\": user_input})\n",
71
+ "\n",
72
+ " system_prompt = {\n",
73
+ " \"role\": \"system\",\n",
74
+ " \"content\": \"Anda adalah asisten AI yang profesional, ramah, dan membantu. Berikan respons dalam bahasa yang digunakan pengguna.\"\n",
75
+ " }\n",
76
+ " \n",
77
+ " messages = [system_prompt] + messages_for_api\n",
78
+ " \n",
79
+ " if len(messages) > 100:\n",
80
+ " messages[:] = messages[-100:] \n",
81
+ "\n",
82
+ " chat_completion = client.chat.completions.create(\n",
83
+ " messages=messages,\n",
84
+ " model=\"llama3-70b-8192\", \n",
85
+ " max_tokens=5000, \n",
86
+ " temperature=0.7, \n",
87
+ " top_p=0.9, \n",
88
+ " stream=False \n",
89
+ " )\n",
90
+ " \n",
91
+ " assistant_response = chat_completion.choices[0].message.content.strip()\n",
92
+ " \n",
93
+ " if not assistant_response:\n",
94
+ " assistant_response = \"Maaf, saya tidak dapat memahami permintaan Anda. Bisakah Anda menjelaskan lebih lanjut?\"\n",
95
+ " \n",
96
+ " logger.info(f\"Input: {user_input} | Respons: {assistant_response}\")\n",
97
+ " return assistant_response\n",
98
+ " \n",
99
+ " except Exception as e:\n",
100
+ " logger.error(f\"Error Groq API: {str(e)}\")\n",
101
+ " return f\"Maaf, terjadi masalah dengan API: {str(e)}. Silakan coba lagi nanti.\"\n",
102
+ "\n",
103
+ "def chatbot_interface(user_input: str, history: list) -> tuple:\n",
104
+ " \"\"\"\n",
105
+ " Fungsi ini dipanggil oleh Gradio setiap kali pengguna mengirim pesan.\n",
106
+ " \n",
107
+ " Args:\n",
108
+ " user_input (str): Pesan yang dimasukkan pengguna.\n",
109
+ " history (list): Riwayat percakapan yang dikelola oleh Gradio.\n",
110
+ "\n",
111
+ " Returns:\n",
112
+ " tuple: (string kosong untuk mengosongkan input textbox, riwayat percakapan yang diperbarui).\n",
113
+ " \"\"\"\n",
114
+ " if not user_input.strip():\n",
115
+ " logger.warning(\"Input kosong diterima dari pengguna.\")\n",
116
+ " return \"\", history\n",
117
+ " \n",
118
+ " response = generate_response_groq(user_input, history)\n",
119
+ " \n",
120
+ " history.append((user_input, response))\n",
121
+ " \n",
122
+ " return \"\", history\n",
123
+ "\n",
124
+ "# Setup antarmuka Gradio\n",
125
+ "with gr.Blocks(css=\"\"\"\n",
126
+ " .chatbot {height: 500px; overflow-y: auto;}\n",
127
+ " .title {font-size: 28px; font-weight: bold; text-align: center; color: #2C3E50; margin-bottom: 10px; font-family: 'Arial', sans-serif;}\n",
128
+ " .subtitle {font-size: 16px; text-align: center; color: #7F8C8D; margin-bottom: 20px;}\n",
129
+ " .button {background-color: #2980B9; color: white; border-radius: 8px; padding: 10px 20px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease;}\n",
130
+ " .button:hover {background-color: #3498DB;} /* Efek hover untuk tombol */\n",
131
+ " .input-box {border-radius: 8px; border: 2px solid #BDC3C7; padding: 10px;}\n",
132
+ "\"\"\") as demo:\n",
133
+ " gr.Markdown('<div class=\"title\">Asisten AI Profesional</div>')\n",
134
+ " gr.Markdown('<div class=\"subtitle\">Ajukan pertanyaan atau mulai percakapan dalam bahasa Indonesia atau Inggris!</div>')\n",
135
+ " \n",
136
+ " chatbot = gr.Chatbot(label=\"Percakapan\", elem_classes=\"chatbot\", show_copy_button=True)\n",
137
+ " \n",
138
+ " user_input = gr.Textbox(\n",
139
+ " label=\"Pesan Anda\",\n",
140
+ " placeholder=\"Contoh: 'Selamat pagi' atau 'Apa kabar?'\",\n",
141
+ " elem_classes=\"input-box\"\n",
142
+ " )\n",
143
+ " \n",
144
+ " submit_button = gr.Button(\"Kirim\", elem_classes=\"button\")\n",
145
+ " \n",
146
+ " submit_button.click(\n",
147
+ " fn=chatbot_interface,\n",
148
+ " inputs=[user_input, chatbot],\n",
149
+ " outputs=[user_input, chatbot]\n",
150
+ " )\n",
151
+ "\n",
152
+ " user_input.submit(\n",
153
+ " fn=chatbot_interface,\n",
154
+ " inputs=[user_input, chatbot],\n",
155
+ " outputs=[user_input, chatbot]\n",
156
+ " )\n",
157
+ "\n",
158
+ "# Luncurkan aplikasi Gradio\n",
159
+ "if __name__ == \"__main__\":\n",
160
+ " demo.launch()\n",
161
+ "\n"
162
+ ]
163
+ },
164
+ {
165
+ "cell_type": "code",
166
+ "execution_count": null,
167
+ "id": "121e553d",
168
+ "metadata": {},
169
+ "outputs": [],
170
+ "source": []
171
+ }
172
+ ],
173
+ "metadata": {
174
+ "kernelspec": {
175
+ "display_name": "base",
176
+ "language": "python",
177
+ "name": "python3"
178
+ },
179
+ "language_info": {
180
+ "codemirror_mode": {
181
+ "name": "ipython",
182
+ "version": 3
183
+ },
184
+ "file_extension": ".py",
185
+ "mimetype": "text/x-python",
186
+ "name": "python",
187
+ "nbconvert_exporter": "python",
188
+ "pygments_lexer": "ipython3",
189
+ "version": "3.12.7"
190
+ }
191
+ },
192
+ "nbformat": 4,
193
+ "nbformat_minor": 5
194
+ }