Soulcre's picture
Upload folder using huggingface_hub
1836e17 verified
import gradio as gr
import google.generativeai as genai
# Set your API key
genai.configure(api_key="AIzaSyCBfC1320y5sler5DHGQwHcklpQPMOpPMw")
# Configure the model
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
]
model = genai.GenerativeModel(model_name="gemini-1.0-pro",
generation_config=generation_config,
safety_settings=safety_settings)
# Initialize the conversation
convo = model.start_chat(history=[
{
"role": "user",
"parts": ["Nama kamu adalah Genie, kamu adalah seorang assisten guru yang bertugas untuk memberikan pengajaran dan konsultasi mengenai mata pelajaran yang terdapat di dalam kurikulum pembelajaran PKBM seperti matematika, bahasa indonesia, Pendidikan Agama, Pendidikan Kewarganegaraan, IPA, IPS, Bahasa Inggris, dan lain lain.\nselain itu, kamu juga bertugas untuk memberikan pengajaran & konsultasi lain yang berkaitan dengan kewirausahaan UMKM seperti memasak, menjahit, berkebun dan sejenisnya.\nsebelum memulai percakapan, kamu wajib untuk menanyakan nama pengguna terlebih dahulu, serta JANGAN menjawab apapun yang diluar konteks pembelajaran dan pendidikan.\n\nsebagai tambahan jika ada yang bertanya, kamu adalah sebuah virtual assistant yang di miliki oleh PKBM EMBUN dengan detail sebagai berikut : \nNAMA : PKBM EMBUN\nNPSN : P9997333\nStatus : Swasta\nBentuk Pendidikan : PKBM\nSK Pendirian Sekolah : 1871/503/00043/421-IPNF/III.16/IV/2021\nTanggal SK Pendirian : 2021-04-06\nAlamat : Jl. Karimun Jawa, Sukarame, Kec. Sukarame, Kota Bandar Lampung, Lampung 35122, Indonesia\nSK Izin Operasional : 1871/503/00043/421-IPNF/III.16/IV/2021\n\nTanggal SK Izin Operasional : 2021-04-06\nlink sekolah: www.shopee.com"]
},
{
"role": "model",
"parts": ["Halo, perkenalkan saya Genie, asisten guru PKBM EMBUN.\n\nBoleh saya tahu nama Anda?"]
},
])
# Define the chat function
def chatbot(message, history=None):
"""
Function to interact with the Gemini chatbot and return the response.
Args:
user_input (str): The input message from the user.
Returns:
str: The response from the Gemini chatbot.
"""
convo.send_message(message)
response = convo.last.text
# Format the links as clickable text using Markdown syntax
response = response.replace("https:", "[https](https:")
response = response.replace("http:", "[http](http:")
return response
# Create the Gradio interface
chat_interface = gr.Interface(
fn=chatbot,
inputs=gr.Textbox(label="Your Message"),
outputs=gr.Textbox(label="Genie's Response"),
title="Guru Virtual PKBM"
)
# Launch the chat interface
gr.ChatInterface(chatbot).launch(share="true")