Hev832 commited on
Commit
ef833e6
·
verified ·
1 Parent(s): 6ae71db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -15
app.py CHANGED
@@ -8,7 +8,7 @@ import time
8
  load_dotenv()
9
 
10
  # Retrieve API key from environment variable
11
- GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # Securely retrieve the API key from the environment
12
 
13
  # Configure Google Gemini API
14
  genai.configure(api_key=GEMINI_API_KEY)
@@ -44,7 +44,7 @@ def generate_response(user_input, chat_history):
44
  )
45
 
46
  # Add user input to history
47
- chat_history.append(("User", user_input))
48
 
49
  # Limit history length to the last 10 messages
50
  chat_history = chat_history[-10:]
@@ -56,29 +56,21 @@ def generate_response(user_input, chat_history):
56
  chat_session = model.start_chat()
57
 
58
  # Send the entire chat history as the first message
59
- messages = [f"{sender}: {message}" for sender, message in chat_history]
60
- response = chat_session.send_message("\n".join(messages))
61
-
62
- # Add the response to the chat history
63
- chat_history.append(("Shadow", response.text))
64
-
65
- return chat_history, chat_history
66
 
67
  except Exception as e:
68
  if attempt < retry_attempts - 1:
69
  time.sleep(2) # Delay before retrying
70
  continue
71
  else:
72
- error_message = f"Error after {retry_attempts} attempts: {str(e)}"
73
- chat_history.append(("Shadow", error_message))
74
- return chat_history, chat_history
75
 
76
  # Build the Gradio interface
77
  with gr.Blocks(theme="Hev832/Applio") as iface:
78
- gr.Markdown("<center><h1>Chat with Shadow The Hedgehog</h1></center>")
79
- chat_input = gr.Textbox(lines=2, label="Talk to Shadow", placeholder="Enter your message here...")
80
  chat_history_state = gr.State([]) # State input for chat history
81
- response_output = gr.Chatbot(label="Response")
82
 
83
  # Define the layout and components
84
  generate_button = gr.Button("Generate Response")
 
8
  load_dotenv()
9
 
10
  # Retrieve API key from environment variable
11
+ GEMINI_API_KEY = "AIzaSyA0SnGcdEuesDusLiM93N68-vaFF14RCYg" # public api
12
 
13
  # Configure Google Gemini API
14
  genai.configure(api_key=GEMINI_API_KEY)
 
44
  )
45
 
46
  # Add user input to history
47
+ chat_history.append(user_input)
48
 
49
  # Limit history length to the last 10 messages
50
  chat_history = chat_history[-10:]
 
56
  chat_session = model.start_chat()
57
 
58
  # Send the entire chat history as the first message
59
+ response = chat_session.send_message("\n".join(chat_history))
60
+ return response.text, chat_history
 
 
 
 
 
61
 
62
  except Exception as e:
63
  if attempt < retry_attempts - 1:
64
  time.sleep(2) # Delay before retrying
65
  continue
66
  else:
67
+ return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
 
 
68
 
69
  # Build the Gradio interface
70
  with gr.Blocks(theme="Hev832/Applio") as iface:
71
+ chat_input = gr.Textbox(lines=2, label="Talk to AI", placeholder="Enter your message here...")
 
72
  chat_history_state = gr.State([]) # State input for chat history
73
+ response_output = gr.Textbox(label="Response")
74
 
75
  # Define the layout and components
76
  generate_button = gr.Button("Generate Response")