Katieearl1513 commited on
Commit
2f595a7
·
verified ·
1 Parent(s): 58e5f5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -27
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import os
2
  import gradio as gr
3
- import google.generativeai as genai
4
- from spellchecker import SpellChecker # Import library for spell checking
5
 
6
  # Configure Gemini AI
7
- api_key = os.getenv("GEMINI_API") # Fetch the Gemini API key securely
8
  genai.configure(api_key=api_key)
9
 
10
  # Define the model with desired generation configuration
@@ -30,25 +29,13 @@ model = genai.GenerativeModel(
30
  system_instruction=system_instruction
31
  )
32
 
33
- # Initialize spell checker
34
- spell = SpellChecker(language='es') # Use Spanish for spell checking
35
-
36
- def correct_spelling(text):
37
- """Correct spelling errors in a given text."""
38
- words = text.split()
39
- corrected_words = [spell.correction(word) or word for word in words]
40
- return " ".join(corrected_words)
41
-
42
  def chatbot(user_input, history, chat_session):
43
- # Correct spelling errors in user input
44
- corrected_input = correct_spelling(user_input)
45
-
46
  # Initialize a new chat session for Gemini AI if history is empty
47
  if chat_session is None:
48
  chat_session = model.start_chat()
49
 
50
  # Send the user query to Gemini AI
51
- response = chat_session.send_message(corrected_input)
52
  reply = response.text
53
 
54
  # Update history
@@ -58,19 +45,24 @@ def chatbot(user_input, history, chat_session):
58
  # Convert to Gradio's format
59
  gradio_history = [[msg["role"], msg["content"]] for msg in history]
60
  return gradio_history, history, chat_session
61
-
62
- #Handling error ---> except Exception as e
63
- #error_message = f"An error occurred: {str(e)}"
64
- #history.append({"role": "assistant", "content": error_message})
65
- #gradio_history = [[msg["role"], msg["content"]] for msg in history]
66
 
67
- #user interface
 
 
 
 
 
 
 
 
 
 
68
  with gr.Blocks() as demo:
69
- chatbot_interface = gr.Chatbot()
70
- state = gr.State([]) # For chat history
71
 
72
- with gr.Row():
73
- txt = gr.Textbox(show_label=False, placeholder="Escribe tu pregunta aquí...")
74
- txt.submit(chatbot, [txt, state], [chatbot_interface, state])
75
 
76
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import google.generativeai as genai
 
4
 
5
  # Configure Gemini AI
6
+ api_key = os.getenv("GEMINI_API") # Fetch the Gemini API key securely
7
  genai.configure(api_key=api_key)
8
 
9
  # Define the model with desired generation configuration
 
29
  system_instruction=system_instruction
30
  )
31
 
 
 
 
 
 
 
 
 
 
32
  def chatbot(user_input, history, chat_session):
 
 
 
33
  # Initialize a new chat session for Gemini AI if history is empty
34
  if chat_session is None:
35
  chat_session = model.start_chat()
36
 
37
  # Send the user query to Gemini AI
38
+ response = chat_session.send_message(user_input)
39
  reply = response.text
40
 
41
  # Update history
 
45
  # Convert to Gradio's format
46
  gradio_history = [[msg["role"], msg["content"]] for msg in history]
47
  return gradio_history, history, chat_session
 
 
 
 
 
48
 
49
+ # Handling errors
50
+ def chatbot_with_error_handling(user_input, history, chat_session):
51
+ try:
52
+ return chatbot(user_input, history, chat_session)
53
+ except Exception as e:
54
+ error_message = f"Ha ocurrido un error: {str(e)}"
55
+ history.append({"role": "assistant", "content": error_message})
56
+ gradio_history = [[msg["role"], msg["content"]] for msg in history]
57
+ return gradio_history, history, chat_session
58
+
59
+ # User interface
60
  with gr.Blocks() as demo:
61
+ chatbot_interface = gr.Chatbot()
62
+ state = gr.State([]) # For chat history
63
 
64
+ with gr.Row():
65
+ txt = gr.Textbox(show_label=False, placeholder="Escribe tu pregunta aquí...")
66
+ txt.submit(chatbot_with_error_handling, [txt, state], [chatbot_interface, state])
67
 
68
  demo.launch()