chatbot / app.py
talha2134's picture
Update app.py
c1a7d60 verified
Raw History Blame Contribute Delete
2.38 kB
import streamlit as st
import google.generativeai as genai
import os
from dotenv import load_dotenv
# ------------------ Load API Key ------------------
load_dotenv()
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
st.error("❌ Please set your GEMINI_API_KEY as a Hugging Face secret.")
else:
genai.configure(api_key=api_key)
# ------------------ App Config ------------------
st.set_page_config(page_title="Trending Research Chatbot", page_icon="🧠", layout="centered")
st.title("🧠 ResearchTrend-Gemini")
st.markdown("### Your AI assistant for trending research papers & videos 📈")
# ------------------ Chat History ------------------
if "messages" not in st.session_state:
st.session_state["messages"] = []
# ------------------ Display Chat ------------------
for msg in st.session_state["messages"]:
if msg["role"] == "user":
st.chat_message("user").markdown(msg["content"])
else:
st.chat_message("assistant").markdown(msg["content"])
# ------------------ User Input ------------------
user_input = st.chat_input("Ask about trending research papers or videos...")
if user_input:
st.chat_message("user").markdown(user_input)
st.session_state["messages"].append({"role": "user", "content": user_input})
# ------------- Super Prompt + User Query -------------
system_prompt = """
You are ResearchTrend-Gemini, an expert AI assistant that finds and explains
the latest trending research papers and YouTube videos on AI, ML, DL, Data Science,
Computer Vision, NLP, and Robotics.
When asked:
- Provide top 5 trending papers with titles, short summaries, topics, and possible arXiv IDs.
- Provide top 3 trending YouTube videos with title, short summary, and approximate upload time.
- If general questions are asked, respond as an AI research expert.
Use clean markdown formatting and emojis for clarity.
"""
full_prompt = system_prompt + "\n\nUser: " + user_input
# ------------------ Gemini Response ------------------
try:
model = genai.GenerativeModel("gemini-2.5-flash-lite")
response = model.generate_content(full_prompt)
output = response.text
st.chat_message("assistant").markdown(output)
st.session_state["messages"].append({"role": "assistant", "content": output})
except Exception as e:
st.error(f"⚠️ Error: {str(e)}")