Katieearl1513 commited on
Commit
cf29b5e
·
verified ·
1 Parent(s): 6818df7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -26
app.py CHANGED
@@ -19,20 +19,6 @@ generation_config = {
19
  }
20
  model_name = "gemini-2.0-flash-exp" # Use the updated model
21
 
22
- # Update the system instruction to meet the requirements
23
- instructions = {
24
- "es": (
25
- "Eres un tutor experto que responde exclusivamente en castellano. "
26
- "Tus respuestas deben centrarse únicamente en temas de estadística básica y "
27
- "todo el código proporcionado debe implementarse exclusivamente en R."
28
- ),
29
- "ca": (
30
- "Ets un tutor expert que respon exclusivament en català. "
31
- "Les teves respostes s'han de centrar únicament en temes d'estadística bàsica i "
32
- "tot el codi proporcionat s'ha d'implementar exclusivament en R."
33
- )
34
- }
35
-
36
  def detect_language(user_input):
37
  try:
38
  lang = detect(user_input)
@@ -57,34 +43,41 @@ def contains_inappropriate_language(text):
57
  return True
58
  return False
59
 
60
- # main code
61
- # Asynchronous chatbot function
 
 
 
 
 
 
 
 
 
 
62
  async def chatbot_async(user_input, history, chat_session):
63
  if contains_inappropriate_language(user_input):
64
  warning = "El lenguaje inapropiado no está permitido. Por favor, formula tu pregunta de manera respetuosa."
65
  history.append({"role": "assistant", "content": warning})
66
- gradio_history = [[msg["role"], msg["content"]] for msg in history]
67
- return gradio_history, history, chat_session
68
 
69
- # Detect language and set appropriate instruction
70
  lang = detect_language(user_input)
71
  system_instruction = instructions[lang]
72
 
73
- # Initialize a new chat session for Gemini AI if history is empty
74
  if chat_session is None:
75
- chat_session = model.start_chat()
 
 
 
 
76
 
77
- # Send the user query to Gemini AI asynchronously
78
  response = await asyncio.to_thread(chat_session.send_message, user_input)
79
  reply = response.text
80
 
81
- # Update history
82
  history.append({"role": "user", "content": user_input})
83
  history.append({"role": "assistant", "content": reply})
84
 
85
- # Convert to Gradio's format
86
- gradio_history = [[msg["role"], msg["content"]] for msg in history]
87
- return gradio_history, history, chat_session
88
 
89
  # Handling errors
90
  def chatbot_with_error_handling(user_input, history, chat_session):
 
19
  }
20
  model_name = "gemini-2.0-flash-exp" # Use the updated model
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def detect_language(user_input):
23
  try:
24
  lang = detect(user_input)
 
43
  return True
44
  return False
45
 
46
+ # Define a custom render function for the chatbot interface
47
+ def render_chat(messages):
48
+ rendered = []
49
+ for message in messages:
50
+ role, content = message["role"], message["content"]
51
+ if role == "user":
52
+ rendered.append((content, None)) # User messages on the left
53
+ else:
54
+ rendered.append((None, content)) # Assistant messages on the right
55
+ return rendered
56
+
57
+ # Updated chatbot function with rendering
58
  async def chatbot_async(user_input, history, chat_session):
59
  if contains_inappropriate_language(user_input):
60
  warning = "El lenguaje inapropiado no está permitido. Por favor, formula tu pregunta de manera respetuosa."
61
  history.append({"role": "assistant", "content": warning})
62
+ return render_chat(history), history, chat_session
 
63
 
 
64
  lang = detect_language(user_input)
65
  system_instruction = instructions[lang]
66
 
 
67
  if chat_session is None:
68
+ chat_session = genai.GenerativeModel(
69
+ model_name=model_name,
70
+ generation_config=generation_config,
71
+ system_instruction=system_instruction
72
+ ).start_chat()
73
 
 
74
  response = await asyncio.to_thread(chat_session.send_message, user_input)
75
  reply = response.text
76
 
 
77
  history.append({"role": "user", "content": user_input})
78
  history.append({"role": "assistant", "content": reply})
79
 
80
+ return render_chat(history), history, chat_session
 
 
81
 
82
  # Handling errors
83
  def chatbot_with_error_handling(user_input, history, chat_session):