Chatbot / app.py
saherPervaiz's picture
Update app.py
6cf385e verified
raw
history blame
4.54 kB
import os
import gradio as gr
from groq import Groq
from googletrans import Translator
# Function to get recommendations from Groq AI based on user input
def get_opportunities(user_interests, user_skills, user_location):
api_key = "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" # Replace with your actual API key
if not api_key:
raise ValueError("API key is missing.")
# Initialize the Groq client with the API key
client = Groq(api_key=api_key)
# Construct the query
query = f"Based on the user's interests in {user_interests}, skills in {user_skills}, and location of {user_location}, find scholarships, internships, online courses, and career advice suitable for them."
# Request to Groq API
response = client.chat.completions.create(
messages=[{"role": "user", "content": query}],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content
# Function to translate text into the selected language
def translate_text(text, target_language):
translator = Translator()
translated = translator.translate(text, dest=target_language)
return translated.text
# Function to get chatbot response
def get_chatbot_response(user_message):
api_key = "your_groq_api_key" # Replace with your actual API key
if not api_key:
raise ValueError("API key is missing.")
# Initialize the Groq client with the API key
client = Groq(api_key=api_key)
# Request to Groq API for chatbot response
response = client.chat.completions.create(
messages=[{"role": "user", "content": user_message}],
model="llama-3.3-70b-versatile",
)
return response.choices[0].message.content
# Gradio interface
with gr.Blocks(css="""
.gradio-container {
background-color: #f0f0f5;
color: #333;
font-family: 'Roboto', sans-serif;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.gradio-container .button {
background-color: #007bff;
color: white;
padding: 12px;
font-size: 16px;
border-radius: 10px;
border: none;
}
.gradio-container .button:hover {
background-color: #0056b3;
}
.gradio-container .textbox {
font-size: 16px;
padding: 12px;
border-radius: 10px;
border: 1px solid #ccc;
}
""") as demo:
gr.Markdown("""
<h1 style="text-align:center;">AI-Powered Opportunity Finder for Youth</h1>
<p style="text-align:center;">Find scholarships, internships, online courses, and career advice based on your interests, skills, and location.</p>
""")
# Sidebar for input fields
with gr.Column():
gr.Markdown("### Provide your details to find opportunities")
interests = gr.Textbox(label="Your Interests (e.g., AI, Robotics, Software Engineering):")
skills = gr.Textbox(label="Your Skills (e.g., Python, Data Science, Web Development):")
location = gr.Textbox(label="Your Location (e.g., Gujrat, Pakistan):")
languages = gr.Dropdown(
label="Select your preferred language:",
choices=["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Urdu"],
value="English"
)
find_button = gr.Button("Find Opportunities")
# Chatbot Section
with gr.Column():
gr.Markdown("### AI Chatbot")
user_message = gr.Textbox(label="Ask anything to the chatbot:", lines=2)
chatbot_output = gr.Textbox(label="Chatbot Response:", interactive=False)
# Function to handle finding opportunities
def find_opportunities_and_chat(interests, skills, location, language, user_message):
# Fetch opportunities
opportunities = get_opportunities(interests, skills, location)
translated_opportunities = translate_text(opportunities, language)
# Get chatbot response
chatbot_response = get_chatbot_response(user_message) if user_message else "Ask me anything!"
return translated_opportunities, chatbot_response
# Connect the button to the function
find_button.click(
find_opportunities_and_chat,
inputs=[interests, skills, location, languages, user_message],
outputs=[gr.Textbox(label="Recommended Opportunities"), chatbot_output]
)
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()