Chatbot1 / app.py
saherPervaiz's picture
Update app.py
9688ff7 verified
raw
history blame
2.85 kB
import gradio as gr
import os
from groq import Groq
from googletrans import Translator
import asyncio
# Function to get recommendations from Groq AI based on user input
def get_opportunities(user_interests, user_skills, user_location):
# Fetch the API key from the environment variable
api_key = "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"
if not api_key:
raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
# 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 (async version)
async def translate_text(text, target_language):
translator = Translator()
translated = await translator.translate(text, dest=target_language)
return translated.text
# Chatbot interface function
def chatbot(input_text, interests, skills, location, language):
if interests and skills and location:
# Fetch recommendations using the Groq API
opportunities = get_opportunities(interests, skills, location)
# Run the async translate function and get the translated text
translated_opportunities = asyncio.run(translate_text(opportunities, language))
return translated_opportunities
else:
return "Please fill all fields: Interests, Skills, and Location."
# Gradio interface components
with gr.Blocks() as demo:
gr.Markdown("# AI-Powered Opportunity Finder for Youth")
with gr.Row():
interests = gr.Textbox(label="Your Interests (e.g., AI, Robotics, Software Engineering):", placeholder="Enter your interests here...")
skills = gr.Textbox(label="Your Skills (e.g., Python, Data Science, Web Development):", placeholder="Enter your skills here...")
location = gr.Textbox(label="Your Location (e.g., Gujrat, Pakistan):", placeholder="Enter your location here...")
language = gr.Dropdown(label="Select your preferred language:", choices=["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Urdu"], value="English")
with gr.Chatbot() as chatbot_output:
submit_button = gr.Button("Find Opportunities")
submit_button.click(chatbot, inputs=[gr.Textbox(), interests, skills, location, language], outputs=chatbot_output)
# Launch the app
demo.launch()