Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chainlit as cl
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# Configure Gemini
|
| 10 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 11 |
+
if not GOOGLE_API_KEY:
|
| 12 |
+
raise ValueError("GOOGLE_API_KEY not found in .env file")
|
| 13 |
+
|
| 14 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 15 |
+
|
| 16 |
+
# Initialize the Gemini Flash model
|
| 17 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
| 18 |
+
|
| 19 |
+
@cl.on_chat_start
|
| 20 |
+
async def start_chat():
|
| 21 |
+
await cl.Message(
|
| 22 |
+
content="Hello! I'm a chatbot powered by Gemini Flash. How can I help you today?"
|
| 23 |
+
).send()
|
| 24 |
+
|
| 25 |
+
@cl.on_message
|
| 26 |
+
async def main(message: cl.Message):
|
| 27 |
+
# Generate response using Gemini
|
| 28 |
+
response = model.generate_content(message.content)
|
| 29 |
+
|
| 30 |
+
# Send response
|
| 31 |
+
await cl.Message(
|
| 32 |
+
content=response.text
|
| 33 |
+
).send()
|