Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,59 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
with open("bio.txt", "r") as f:
|
| 5 |
bio = f.read()
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
|
| 5 |
+
# Get Gemini API key from Hugging Face Secrets
|
| 6 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Configure Gemini
|
| 9 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 10 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 11 |
+
|
| 12 |
+
# Load bio context
|
| 13 |
with open("bio.txt", "r") as f:
|
| 14 |
bio = f.read()
|
| 15 |
|
| 16 |
+
# Define allowed topics
|
| 17 |
+
ALLOWED_TOPICS = [
|
| 18 |
+
"ai", "machine learning", "ml", "data science", "blockchain", "web3",
|
| 19 |
+
"chinese", "turing project", "deonai", "rust", "llm", "llms", "financai", "eucossa"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def is_on_topic(message: str):
|
| 23 |
+
message = message.lower()
|
| 24 |
+
return any(topic in message for topic in ALLOWED_TOPICS)
|
| 25 |
+
|
| 26 |
+
# Define DeonAI logic
|
| 27 |
+
def chat_with_deonai(message, history):
|
| 28 |
+
if not is_on_topic(message):
|
| 29 |
+
return "I'm DeonAI! I'm specialized in Chinese’s work, AI, ML, and Web3. Please ask me something in those areas. 😊"
|
| 30 |
+
|
| 31 |
+
prompt = f"""
|
| 32 |
+
You are DeonAI, a creative, helpful AI assistant embedded in a portfolio website.
|
| 33 |
+
|
| 34 |
+
You ONLY discuss topics related to:
|
| 35 |
+
- Chinese (the portfolio owner)
|
| 36 |
+
- His biography and projects
|
| 37 |
+
- AI, Machine Learning, LLMs, Blockchain, and Web3
|
| 38 |
+
- Ongoing projects like Turing Project, DeonAI, FinancAI, etc.
|
| 39 |
+
|
| 40 |
+
Here is your background info:
|
| 41 |
+
{bio}
|
| 42 |
+
|
| 43 |
+
Now respond helpfully, concisely, and informatively.
|
| 44 |
+
|
| 45 |
+
User: {message}
|
| 46 |
+
DeonAI:
|
| 47 |
+
"""
|
| 48 |
+
try:
|
| 49 |
+
response = model.generate_content(prompt)
|
| 50 |
+
return response.text.strip()
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}"
|
| 53 |
+
|
| 54 |
+
# Gradio chat interface
|
| 55 |
+
gr.ChatInterface(
|
| 56 |
+
fn=chat_with_deonai,
|
| 57 |
+
title="🤖 DeonAI",
|
| 58 |
+
description="Ask me anything about Chinese, AI, ML, or Web3! I'm your smart assistant 👨🏽💻",
|
| 59 |
+
).launch()
|