palbha's picture
Rename app.py to yapp.py
191fd53 verified
import streamlit as st
# Import from the correct package
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.tools import Tool
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.tools import DuckDuckGoSearchRun
import os
# Configure LangChain LLM with Gemini
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=os.getenv("gemini_api"))
# Use DuckDuckGo Search (no API key needed)
ddgs = DuckDuckGoSearchRun()
search_tool = Tool(
name="Web Search",
func=ddgs.run,
description="Searches the web for relevant certification information."
)
# Create LangChain agent
agent = initialize_agent(
tools=[search_tool],
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
def azure_cert_bot(cert_name):
query = f"Microsoft Azure {cert_name} certification curriculum site:microsoft.com"
search_results = ddgs.run(query).split("\n")
prompt = f"Based on the following curriculum details, generate key questions and answers in markdown format for the {cert_name} certification exam. Do not include any metadata or unnecessary text, only return the formatted Q&A:\n{search_results}"
response = llm.invoke(prompt)
try:
response_text = response.get("content", "No response generated.") if isinstance(response, dict) else response
response_text = "\n".join([line for line in response_text.split("\n") if not line.lower().startswith("content=") and "metadata" not in line.lower()])
except Exception as e:
response_text = f"Error processing response: {str(e)}"
return search_results, response_text
# Streamlit UI Enhancements
st.set_page_config(page_title="Azure Certification Prep Assistant", layout="wide")
# Custom Styling
st.markdown("""
<style>
body {
font-family: 'Arial', sans-serif;
}
.stApp {
background-color: #f5f7fa;
}
.title {
text-align: center;
color: #1f77b4;
font-size: 36px;
font-weight: bold;
}
.subheader {
color: #ff5733;
font-size: 24px;
font-weight: bold;
}
.markdown-text-container {
background-color: white;
padding: 15px;
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
}
</style>
""", unsafe_allow_html=True)
st.markdown("<div class='title'>Azure Certification Prep Assistant</div>", unsafe_allow_html=True)
cert_name = st.text_input("Enter Azure Certification Name (e.g., AZ-900)", "")
if st.button("Get Certification Details"):
if cert_name:
links, qa_content = azure_cert_bot(cert_name)
st.markdown("<div class='subheader'>Certification Links & Curriculum</div>", unsafe_allow_html=True)
for link in links:
if link.strip():
st.markdown(f"<div class='markdown-text-container'>- <a href='{link}' target='_blank'>{link}</a></div>", unsafe_allow_html=True)
st.markdown("<div class='subheader'>Exam Questions & Answers</div>", unsafe_allow_html=True)
st.markdown(f"<div class='markdown-text-container'>{qa_content}</div>", unsafe_allow_html=True)
else:
st.warning("Please enter a certification name.")