Anmol3263 commited on
Commit
1a22ee3
·
verified ·
1 Parent(s): cb87c59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -2,30 +2,38 @@ import gradio as gr
2
  import google.generativeai as genai
3
  import os
4
 
5
- # Secure API key
6
- genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
 
 
 
 
 
7
 
8
- # Model
9
  model = genai.GenerativeModel("gemini-1.5-flash")
10
 
11
- # Chat memory
12
  chat_session = model.start_chat(history=[])
13
 
14
- # Chat function
15
  def chat(user_input, history):
16
  try:
 
 
 
17
  response = chat_session.send_message(user_input)
18
- reply = response.text
 
19
  history.append((user_input, reply))
20
  return history, history
 
21
  except Exception as e:
22
- return str(e), history
 
 
23
 
24
- # UI (customized)
25
  iface = gr.ChatInterface(
26
  fn=chat,
27
  title="Jarvis AI 🤖",
28
- description="Internal AI Assistant for Team",
29
  )
30
 
31
- iface.launch()
 
2
  import google.generativeai as genai
3
  import os
4
 
5
+ # API key check
6
+ api_key = os.getenv("GEMINI_API_KEY")
7
+
8
+ if not api_key:
9
+ raise ValueError("API key not found! Check Hugging Face Variables.")
10
+
11
+ genai.configure(api_key=api_key)
12
 
 
13
  model = genai.GenerativeModel("gemini-1.5-flash")
14
 
 
15
  chat_session = model.start_chat(history=[])
16
 
 
17
  def chat(user_input, history):
18
  try:
19
+ if not user_input:
20
+ return history, history
21
+
22
  response = chat_session.send_message(user_input)
23
+ reply = response.text if response.text else "No response from AI"
24
+
25
  history.append((user_input, reply))
26
  return history, history
27
+
28
  except Exception as e:
29
+ history.append((user_input, f"Error: {str(e)}"))
30
+ return history, history
31
+
32
 
 
33
  iface = gr.ChatInterface(
34
  fn=chat,
35
  title="Jarvis AI 🤖",
36
+ description="Internal AI Assistant"
37
  )
38
 
39
+ iface.launch(server_name="0.0.0.0", server_port=7860)