Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,41 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
|
|
3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
4 |
from langchain.tools import Tool
|
5 |
-
from
|
6 |
-
import
|
|
|
7 |
import os
|
8 |
|
9 |
-
# Configure LangChain LLM with Gemini 2.0
|
10 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp", google_api_key=os.getenv("gemini_api"))
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def azure_cert_bot(cert_name):
|
21 |
query = f"Microsoft Azure {cert_name} certification curriculum site:microsoft.com"
|
22 |
-
|
23 |
-
|
24 |
-
search_tool = Tool(
|
25 |
-
name="Google Search",
|
26 |
-
func=google_search.run,
|
27 |
-
description="Search the web for information about certifications."
|
28 |
-
)
|
29 |
-
# Perform Google Search
|
30 |
-
search_results = search_tool.run(query)
|
31 |
-
|
32 |
-
# Generate Q&A using Gemini
|
33 |
-
prompt = (
|
34 |
-
f"Based on the following curriculum details, generate key questions and answers in markdown format for the {cert_name} certification exam:\n\n{search_results}"
|
35 |
-
)
|
36 |
response = llm.invoke(prompt)
|
37 |
|
38 |
try:
|
39 |
response_text = response.get("content", "No response generated.") if isinstance(response, dict) else response
|
|
|
40 |
except Exception as e:
|
41 |
response_text = f"Error processing response: {str(e)}"
|
42 |
|
@@ -82,9 +81,11 @@ if st.button("Get Certification Details"):
|
|
82 |
links, qa_content = azure_cert_bot(cert_name)
|
83 |
|
84 |
st.markdown("<div class='subheader'>Certification Links & Curriculum</div>", unsafe_allow_html=True)
|
85 |
-
|
|
|
|
|
86 |
|
87 |
st.markdown("<div class='subheader'>Exam Questions & Answers</div>", unsafe_allow_html=True)
|
88 |
st.markdown(f"<div class='markdown-text-container'>{qa_content}</div>", unsafe_allow_html=True)
|
89 |
else:
|
90 |
-
st.warning("Please enter a certification name.")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
# Import from the correct package
|
3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
4 |
from langchain.tools import Tool
|
5 |
+
from langchain.agents import initialize_agent
|
6 |
+
from langchain.agents import AgentType
|
7 |
+
from langchain.tools import DuckDuckGoSearchRun
|
8 |
import os
|
9 |
|
|
|
|
|
10 |
|
11 |
+
# Configure LangChain LLM with Gemini
|
12 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash", google_api_key=os.getenv("gemini_api"))
|
13 |
+
|
14 |
+
# Use DuckDuckGo Search (no API key needed)
|
15 |
+
ddgs = DuckDuckGoSearchRun()
|
16 |
+
search_tool = Tool(
|
17 |
+
name="Web Search",
|
18 |
+
func=ddgs.run,
|
19 |
+
description="Searches the web for relevant certification information."
|
20 |
+
)
|
21 |
+
|
22 |
+
# Create LangChain agent
|
23 |
+
agent = initialize_agent(
|
24 |
+
tools=[search_tool],
|
25 |
+
llm=llm,
|
26 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
27 |
+
verbose=True
|
28 |
+
)
|
29 |
|
30 |
def azure_cert_bot(cert_name):
|
31 |
query = f"Microsoft Azure {cert_name} certification curriculum site:microsoft.com"
|
32 |
+
search_results = ddgs.run(query).split("\n")
|
33 |
+
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}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
response = llm.invoke(prompt)
|
35 |
|
36 |
try:
|
37 |
response_text = response.get("content", "No response generated.") if isinstance(response, dict) else response
|
38 |
+
response_text = "\n".join([line for line in response_text.split("\n") if not line.lower().startswith("content=") and "metadata" not in line.lower()])
|
39 |
except Exception as e:
|
40 |
response_text = f"Error processing response: {str(e)}"
|
41 |
|
|
|
81 |
links, qa_content = azure_cert_bot(cert_name)
|
82 |
|
83 |
st.markdown("<div class='subheader'>Certification Links & Curriculum</div>", unsafe_allow_html=True)
|
84 |
+
for link in links:
|
85 |
+
if link.strip():
|
86 |
+
st.markdown(f"<div class='markdown-text-container'>- <a href='{link}' target='_blank'>{link}</a></div>", unsafe_allow_html=True)
|
87 |
|
88 |
st.markdown("<div class='subheader'>Exam Questions & Answers</div>", unsafe_allow_html=True)
|
89 |
st.markdown(f"<div class='markdown-text-container'>{qa_content}</div>", unsafe_allow_html=True)
|
90 |
else:
|
91 |
+
st.warning("Please enter a certification name.")
|