Chatbot / app.py
saherPervaiz's picture
Update app.py
2038bb2 verified
raw
history blame
4.07 kB
import os
import streamlit as st
from groq import Groq
from googletrans import Translator
import asyncio
# Initialize the Groq client
client = Groq(api_key="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")
# Function to get recommendations from Groq API based on user input
def get_opportunities(user_query):
# Create a chat completion request
chat_completion = client.chat.completions.create(
messages=[{"role": "user", "content": user_query}],
model="llama-3.3-70b-versatile"
)
# Return the response content
return chat_completion.choices[0].message.content
# Function to translate text into the selected language (async version)
async def translate_text(text, target_language):
translator = Translator()
translated = await translator.translate(text, dest=target_language)
return translated.text
# Streamlit App Interface
st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
st.title("AI-Powered Opportunity Finder for Youth")
# Custom CSS for improving the UI
st.markdown("""
<style>
.css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
.css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
.css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
.css-1f4y1re {font-size: 1.2rem;}
</style>
""", unsafe_allow_html=True)
# Sidebar for input fields
st.sidebar.header("Ask the AI Chatbot for Opportunities")
# Language selection
languages = {
"English": "English",
"Spanish": "Spanish",
"French": "French",
"German": "German",
"Italian": "Italian",
"Chinese": "Chinese",
"Japanese": "Japanese",
"Urdu": "Urdu"
}
selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
# Chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
if message["role"] == "user":
st.markdown(f"**You:** {message['content']}")
else:
st.markdown(f"**AI:** {message['content']}")
# Input box for user query
user_query = st.text_input("Ask me about scholarships, internships, or online courses:")
# Button to send message to chatbot
if st.button("Send"):
if user_query:
# Append user query to chat history
st.session_state.messages.append({"role": "user", "content": user_query})
with st.spinner("Fetching opportunities..."):
try:
# Get the opportunity details based on user query
opportunities = get_opportunities(user_query)
# Run the async translate function and get the translated text
translated_opportunities = asyncio.run(translate_text(opportunities, languages[selected_language]))
# Append AI response to chat history
st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
except Exception as e:
st.error(f"Error: {e}")
# Scroll to the latest message
st.experimental_rerun()
else:
st.error("Please enter a query.")
# Add a footer with contact info and clickable links
st.markdown("""
<footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
<p>Powered by Groq, Google Translate, and Streamlit</p>
<p>For more opportunities, visit:</p>
<ul style="list-style-type:none; padding: 0;">
<li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
<li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
<li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
<li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
</ul>
<p>Contact: [email protected]</p>
</footer>
""", unsafe_allow_html=True)