Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import logging
|
| 3 |
+
import os
|
| 4 |
+
from groq import Groq
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
logging.basicConfig(level=logging.INFO, filename="chatbot_v10.log", filemode="a",
|
| 10 |
+
format="%(asctime)s - %(levelname)s - %(message)s")
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
client = Groq(api_key=os.getenv('GROQ_API_KEY'))
|
| 14 |
+
|
| 15 |
+
def generate_response_groq(user_input: str, current_history: list) -> str:
|
| 16 |
+
"""
|
| 17 |
+
Menghasilkan respons dari Groq API berdasarkan input pengguna dan riwayat percakapan.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
user_input (str): Pesan yang dimasukkan oleh pengguna.
|
| 21 |
+
current_history (list): Riwayat percakapan Gradio, berupa list of tuples (user_msg, bot_msg).
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
str: Respons dari asisten AI.
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
messages_for_api = []
|
| 28 |
+
for user_msg, bot_msg in current_history:
|
| 29 |
+
messages_for_api.append({"role": "user", "content": user_msg})
|
| 30 |
+
messages_for_api.append({"role": "assistant", "content": bot_msg})
|
| 31 |
+
|
| 32 |
+
messages_for_api.append({"role": "user", "content": user_input})
|
| 33 |
+
|
| 34 |
+
system_prompt = {
|
| 35 |
+
"role": "system",
|
| 36 |
+
"content": "Anda adalah asisten AI yang profesional, ramah, dan membantu. Berikan respons dalam bahasa yang digunakan pengguna."
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
messages = [system_prompt] + messages_for_api
|
| 40 |
+
|
| 41 |
+
if len(messages) > 100:
|
| 42 |
+
messages[:] = messages[-100:]
|
| 43 |
+
|
| 44 |
+
chat_completion = client.chat.completions.create(
|
| 45 |
+
messages=messages,
|
| 46 |
+
model="llama3-70b-8192",
|
| 47 |
+
max_tokens=5000,
|
| 48 |
+
temperature=0.7,
|
| 49 |
+
top_p=0.9,
|
| 50 |
+
stream=False
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
assistant_response = chat_completion.choices[0].message.content.strip()
|
| 54 |
+
|
| 55 |
+
if not assistant_response:
|
| 56 |
+
assistant_response = "Maaf, saya tidak dapat memahami permintaan Anda. Bisakah Anda menjelaskan lebih lanjut?"
|
| 57 |
+
|
| 58 |
+
logger.info(f"Input: {user_input} | Respons: {assistant_response}")
|
| 59 |
+
return assistant_response
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.error(f"Error Groq API: {str(e)}")
|
| 63 |
+
return f"Maaf, terjadi masalah dengan API: {str(e)}. Silakan coba lagi nanti."
|
| 64 |
+
|
| 65 |
+
def chatbot_interface(user_input: str, history: list) -> tuple:
|
| 66 |
+
"""
|
| 67 |
+
Fungsi ini dipanggil oleh Gradio setiap kali pengguna mengirim pesan.
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
user_input (str): Pesan yang dimasukkan pengguna.
|
| 71 |
+
history (list): Riwayat percakapan yang dikelola oleh Gradio.
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
tuple: (string kosong untuk mengosongkan input textbox, riwayat percakapan yang diperbarui).
|
| 75 |
+
"""
|
| 76 |
+
if not user_input.strip():
|
| 77 |
+
logger.warning("Input kosong diterima dari pengguna.")
|
| 78 |
+
return "", history
|
| 79 |
+
|
| 80 |
+
response = generate_response_groq(user_input, history)
|
| 81 |
+
|
| 82 |
+
history.append((user_input, response))
|
| 83 |
+
|
| 84 |
+
return "", history
|
| 85 |
+
|
| 86 |
+
# Setup antarmuka Gradio
|
| 87 |
+
with gr.Blocks(css="""
|
| 88 |
+
.chatbot {height: 500px; overflow-y: auto;}
|
| 89 |
+
.title {font-size: 28px; font-weight: bold; text-align: center; color: #2C3E50; margin-bottom: 10px; font-family: 'Arial', sans-serif;}
|
| 90 |
+
.subtitle {font-size: 16px; text-align: center; color: #7F8C8D; margin-bottom: 20px;}
|
| 91 |
+
.button {background-color: #2980B9; color: white; border-radius: 8px; padding: 10px 20px; font-size: 16px; cursor: pointer; transition: background-color 0.3s ease;}
|
| 92 |
+
.button:hover {background-color: #3498DB;} /* Efek hover untuk tombol */
|
| 93 |
+
.input-box {border-radius: 8px; border: 2px solid #BDC3C7; padding: 10px;}
|
| 94 |
+
""") as demo:
|
| 95 |
+
gr.Markdown('<div class="title">Asisten AI Profesional</div>')
|
| 96 |
+
gr.Markdown('<div class="subtitle">Ajukan pertanyaan atau mulai percakapan dalam bahasa Indonesia atau Inggris!</div>')
|
| 97 |
+
|
| 98 |
+
chatbot = gr.Chatbot(label="Percakapan", elem_classes="chatbot", show_copy_button=True)
|
| 99 |
+
|
| 100 |
+
user_input = gr.Textbox(
|
| 101 |
+
label="Pesan Anda",
|
| 102 |
+
placeholder="Contoh: 'Selamat pagi' atau 'Apa kabar?'",
|
| 103 |
+
elem_classes="input-box"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
submit_button = gr.Button("Kirim", elem_classes="button")
|
| 107 |
+
|
| 108 |
+
submit_button.click(
|
| 109 |
+
fn=chatbot_interface,
|
| 110 |
+
inputs=[user_input, chatbot],
|
| 111 |
+
outputs=[user_input, chatbot]
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
user_input.submit(
|
| 115 |
+
fn=chatbot_interface,
|
| 116 |
+
inputs=[user_input, chatbot],
|
| 117 |
+
outputs=[user_input, chatbot]
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Luncurkan aplikasi Gradio
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|
| 123 |
+
|