Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,64 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from groq import Groq
|
4 |
+
from googletrans import Translator
|
5 |
+
import asyncio
|
6 |
|
7 |
+
# Function to get recommendations from Groq AI based on user input
|
8 |
+
def get_opportunities(user_interests, user_skills, user_location):
|
9 |
+
# Fetch the API key from the environment variable
|
10 |
+
api_key = "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"
|
11 |
+
|
12 |
+
if not api_key:
|
13 |
+
raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
|
14 |
+
|
15 |
+
# Initialize the Groq client with the API key
|
16 |
+
client = Groq(api_key=api_key)
|
17 |
+
|
18 |
+
# Construct the query
|
19 |
+
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."
|
20 |
+
|
21 |
+
# Request to Groq API
|
22 |
+
response = client.chat.completions.create(
|
23 |
+
messages=[{"role": "user", "content": query}],
|
24 |
+
model="llama-3.3-70b-versatile",
|
25 |
+
)
|
26 |
+
|
27 |
+
return response.choices[0].message.content
|
28 |
|
29 |
+
# Function to translate text into the selected language (async version)
|
30 |
+
async def translate_text(text, target_language):
|
31 |
+
translator = Translator()
|
32 |
+
translated = await translator.translate(text, dest=target_language)
|
33 |
+
return translated.text
|
34 |
|
35 |
+
# Chatbot interface function
|
36 |
+
def chatbot(input_text, interests, skills, location, language):
|
37 |
+
if interests and skills and location:
|
38 |
+
# Fetch recommendations using the Groq API
|
39 |
+
opportunities = get_opportunities(interests, skills, location)
|
40 |
+
|
41 |
+
# Run the async translate function and get the translated text
|
42 |
+
translated_opportunities = asyncio.run(translate_text(opportunities, language))
|
43 |
+
|
44 |
+
return translated_opportunities
|
45 |
+
else:
|
46 |
+
return "Please fill all fields: Interests, Skills, and Location."
|
47 |
|
48 |
+
# Gradio interface components
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# AI-Powered Opportunity Finder for Youth")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
interests = gr.Textbox(label="Your Interests (e.g., AI, Robotics, Software Engineering):", placeholder="Enter your interests here...")
|
54 |
+
skills = gr.Textbox(label="Your Skills (e.g., Python, Data Science, Web Development):", placeholder="Enter your skills here...")
|
55 |
+
location = gr.Textbox(label="Your Location (e.g., Gujrat, Pakistan):", placeholder="Enter your location here...")
|
56 |
+
|
57 |
+
language = gr.Dropdown(label="Select your preferred language:", choices=["English", "Spanish", "French", "German", "Italian", "Chinese", "Japanese", "Urdu"], value="English")
|
58 |
+
|
59 |
+
with gr.Chatbot() as chatbot_output:
|
60 |
+
submit_button = gr.Button("Find Opportunities")
|
61 |
+
submit_button.click(chatbot, inputs=[gr.Textbox(), interests, skills, location, language], outputs=chatbot_output)
|
62 |
|
63 |
+
# Launch the app
|
64 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|