Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,34 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
print("API KEY:", api_key)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
raise ValueError("GEMINI_API_KEY not found in environment!")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
genai.configure(api_key=api_key)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 18 |
|
| 19 |
def chat(message, history):
|
| 20 |
try:
|
| 21 |
response = model.generate_content(message)
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
reply =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
return history, history
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
-
|
| 31 |
-
print(error_msg)
|
| 32 |
-
history.append((message, error_msg))
|
| 33 |
-
return history, history
|
| 34 |
|
| 35 |
|
| 36 |
-
demo = gr.ChatInterface(
|
| 37 |
-
fn=chat,
|
| 38 |
-
title="Jarvis AI π€",
|
| 39 |
-
description="Internal AI Assistant"
|
| 40 |
-
)
|
| 41 |
|
| 42 |
demo.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 Exception("β GEMINI_API_KEY missing in Secrets")
|
|
|
|
| 10 |
|
| 11 |
+
genai.configure(api_key=API_KEY)
|
|
|
|
| 12 |
|
| 13 |
+
# simple working model
|
| 14 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 15 |
|
| 16 |
def chat(message, history):
|
| 17 |
try:
|
| 18 |
response = model.generate_content(message)
|
| 19 |
|
| 20 |
+
# safest extraction
|
| 21 |
+
reply = ""
|
| 22 |
+
if hasattr(response, "text") and response.text:
|
| 23 |
+
reply = response.text
|
| 24 |
+
else:
|
| 25 |
+
reply = "β οΈ No response from model"
|
| 26 |
|
| 27 |
+
return reply
|
|
|
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
+
return f"π₯ ERROR: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
+
demo = gr.ChatInterface(fn=chat)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
demo.launch()
|