Update app.py
Browse files17 unmodified lines181920212223242526272829303132333435363738394041421 unmodified line4445464748495017 unmodified linessystem_instruction = "You are an expert tutor assisting students with their homework"def chatbot(user_input, history):# Initialisation of the model to prevent repeated model creationif 'model' not in chatbot.__dict__:chatbot.model = genai.GenerativeModel(model_name=model_name,generation_config=generation_config,system_instruction=system_instruction,)if not history: # Start a new chat session if history is emptychat_session = chatbot.model.start_chat()history = [] #Initialize the history properly in the Gradio stateelse:chat_session = history[-1]['chat_session']# Send user input to the Gemini AI modelresponse = chat_session.send_message(user_input)reply = response.text# Update chat historyhistory.append({"role": "user", "content": user_input})history.append({"role": "assistant", "content": reply, "chat_session": chat_session})1 unmodified linegradio_history = [[msg["role"], msg["content"]] for msg in history]return gradio_history, history#user interface of Gradio web app is created structured in blocks.#containing typical chatbot setting, with history saved and textbox to make entries#If the user makes an input “txt.sumit()”, the function “chatbot()” is called and the output is displayed in the chat window “chatbot_interface”17 unmodified lines18192021222324252627282930313233341 unmodified line36373839404117 unmodified linessystem_instruction = "You are an expert tutor assisting students with their homework"def chatbot(user_input, history):# Initialize a new chat session for Gemini AI if history is emptyif len(history) == 0 or not isinstance(history[-1].get("chat_session"), genai.ChatSession):chat_session = model.start_chat()else:chat_session = history[-1]["chat_session"]# Send the user query to Gemini AIresponse = chat_session.send_message(user_input)reply = response.text# Update historyhistory.append({"role": "user", "content": user_input})history.append({"role": "assistant", "content": reply, "chat_session": chat_session})1 unmodified linegradio_history = [[msg["role"], msg["content"]] for msg in history]return gradio_history, history#user interface of Gradio web app is created structured in blocks.#containing typical chatbot setting, with history saved and textbox to make entries#If the user makes an input “txt.sumit()”, the function “chatbot()” is called and the output is displayed in the chat window “chatbot_interface”
app.py+6-15