saherPervaiz commited on
Commit
064ac70
·
verified ·
1 Parent(s): c5bd48f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -93
app.py CHANGED
@@ -1,124 +1,80 @@
1
  import os
2
- import requests
3
  import streamlit as st
4
  from deep_translator import GoogleTranslator
5
- import asyncio
6
 
7
- # Ensure you have the correct API URL for Groq and the key is set
8
- GROQ_API_URL = "https://api.groq.com/v1/completions" # Replace with the actual endpoint if different
9
  API_KEY = os.environ.get("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")
10
-
11
  if not API_KEY:
12
- raise ValueError("API key is missing. Make sure to set the GROQ_API_KEY environment variable.")
13
-
14
- # Function to get recommendations from Groq API based on user input
15
- def get_opportunities(user_query):
16
- headers = {
17
- "Authorization": f"Bearer {API_KEY}",
18
- "Content-Type": "application/json"
19
- }
20
-
21
- payload = {
22
- "model": "llama-3.3-70b-versatile", # Example model, replace with the actual model you want
23
- "messages": [{"role": "user", "content": user_query}],
24
- }
25
-
26
- # Send the request to the Groq API
27
- response = requests.post(GROQ_API_URL, json=payload, headers=headers)
28
-
29
- if response.status_code == 200:
30
- return response.json() # Adjust as per Groq's response format
31
- else:
32
- raise ValueError(f"Error fetching data from Groq API: {response.status_code}, {response.text}")
33
 
34
- # Function to translate text into the selected language (async version)
35
- async def translate_text(text, target_language):
36
- translated = GoogleTranslator(source='auto', target=target_language).translate(text)
37
- return translated
38
 
39
- # Streamlit App Interface
40
- st.set_page_config(page_title="AI-Powered Opportunity Finder", page_icon=":bulb:", layout="wide")
41
  st.title("AI-Powered Opportunity Finder for Youth")
42
 
43
- # Custom CSS for improving the UI
44
- st.markdown("""
45
- <style>
46
- .css-1v0mbdj {padding-top: 30px; font-size: 1.5rem;}
47
- .css-1wa3m6h {padding: 10px; background-color: #f0f0f5;}
48
- .css-1w4t6pm {border: 1px solid #ccc; padding: 10px; background-color: #fff;}
49
- .css-1f4y1re {font-size: 1.2rem;}
50
- </style>
51
- """, unsafe_allow_html=True)
52
 
53
- # Sidebar for input fields
54
- st.sidebar.header("Ask the AI Chatbot for Opportunities")
55
-
56
- # Language selection
57
  languages = {
58
  "English": "en",
59
  "Spanish": "es",
60
  "French": "fr",
61
  "German": "de",
62
  "Italian": "it",
63
- "Chinese": "zh",
64
  "Japanese": "ja",
65
- "Urdu": "ur"
66
  }
67
-
68
  selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
69
 
70
- # Chat history
71
- if "messages" not in st.session_state:
72
- st.session_state.messages = []
73
-
74
- # Display chat history
75
- for message in st.session_state.messages:
76
- if message["role"] == "user":
77
- st.markdown(f"**You:** {message['content']}")
78
- else:
79
- st.markdown(f"**AI:** {message['content']}")
 
 
 
 
 
80
 
81
- # Input box for user query
82
- user_query = st.text_input("Ask me about scholarships, internships, or online courses:")
 
 
 
 
 
83
 
84
- # Button to send message to chatbot
85
- if st.button("Send"):
86
- if user_query:
87
- # Append user query to chat history
88
- st.session_state.messages.append({"role": "user", "content": user_query})
89
-
90
  with st.spinner("Fetching opportunities..."):
91
- try:
92
- # Get the opportunity details based on user query
93
- opportunities_response = get_opportunities(user_query)
94
-
95
- # Extract the response content, modify based on actual response format
96
- opportunities_text = opportunities_response.get("choices", [{}])[0].get("message", {}).get("content", "No data found.")
97
-
98
- # Run the async translate function and get the translated text
99
- translated_opportunities = asyncio.run(translate_text(opportunities_text, languages[selected_language]))
100
-
101
- # Append AI response to chat history
102
- st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
103
- except Exception as e:
104
- st.error(f"Error: {e}")
105
-
106
- # Scroll to the latest message
107
- st.experimental_rerun()
108
  else:
109
- st.error("Please enter a query.")
110
 
111
- # Add a footer with contact info and clickable links
112
  st.markdown("""
113
  <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
114
- <p>Powered by Groq, Google Translate (deep-translator), and Streamlit</p>
115
- <p>For more opportunities, visit:</p>
116
- <ul style="list-style-type:none; padding: 0;">
117
- <li><a href="https://www.groq.com" target="_blank">Groq - AI Solutions</a></li>
118
- <li><a href="https://www.scholarships.com" target="_blank">Scholarships.com</a></li>
119
- <li><a href="https://www.coursera.org" target="_blank">Coursera - Online Courses</a></li>
120
- <li><a href="https://www.linkedin.com/jobs" target="_blank">LinkedIn Jobs</a></li>
121
- </ul>
122
  <p>Contact: [email protected]</p>
123
  </footer>
124
  """, unsafe_allow_html=True)
 
1
  import os
 
2
  import streamlit as st
3
  from deep_translator import GoogleTranslator
4
+ from groq import Client # Assuming correct Groq API client import
5
 
6
+ # Ensure the API key is set
 
7
  API_KEY = os.environ.get("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")
 
8
  if not API_KEY:
9
+ st.error("API key is missing. Please set the GROQ_API_KEY environment variable.")
10
+ st.stop()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Initialize the Groq client
13
+ client = Client(api_key=API_KEY)
 
 
14
 
15
+ # Streamlit App
16
+ st.set_page_config(page_title="AI-Powered Opportunity Finder", layout="wide")
17
  st.title("AI-Powered Opportunity Finder for Youth")
18
 
19
+ # Sidebar for inputs
20
+ st.sidebar.header("Provide your details to find opportunities")
21
+ interests = st.sidebar.text_input("Your Interests (e.g., AI, Robotics, Software Engineering):")
22
+ skills = st.sidebar.text_input("Your Skills (e.g., Python, Data Science, Web Development):")
23
+ location = st.sidebar.text_input("Your Location (e.g., Gujrat, Pakistan):")
 
 
 
 
24
 
25
+ # Language selection for translation
 
 
 
26
  languages = {
27
  "English": "en",
28
  "Spanish": "es",
29
  "French": "fr",
30
  "German": "de",
31
  "Italian": "it",
32
+ "Chinese": "zh-cn",
33
  "Japanese": "ja",
34
+ "Urdu": "ur",
35
  }
 
36
  selected_language = st.sidebar.selectbox("Select your preferred language:", list(languages.keys()))
37
 
38
+ # Main functionality
39
+ def fetch_opportunities(user_interests, user_skills, user_location):
40
+ query = (
41
+ f"Based on the user's interests in {user_interests}, skills in {user_skills}, "
42
+ f"and location of {user_location}, find scholarships, internships, online courses, and career advice."
43
+ )
44
+ try:
45
+ response = client.chat.completions.create(
46
+ messages=[{"role": "user", "content": query}],
47
+ model="llama-3.3-70b-versatile",
48
+ )
49
+ return response.choices[0].message.content
50
+ except Exception as e:
51
+ st.error(f"Error fetching opportunities: {e}")
52
+ return None
53
 
54
+ def translate_text(text, target_language):
55
+ try:
56
+ translator = GoogleTranslator(target=target_language)
57
+ return translator.translate(text)
58
+ except Exception as e:
59
+ st.error(f"Error translating text: {e}")
60
+ return text
61
 
62
+ # Button to fetch opportunities
63
+ if st.sidebar.button("Find Opportunities"):
64
+ if interests and skills and location:
 
 
 
65
  with st.spinner("Fetching opportunities..."):
66
+ opportunities = fetch_opportunities(interests, skills, location)
67
+ if opportunities:
68
+ translated_opportunities = translate_text(opportunities, languages[selected_language])
69
+ st.subheader("Recommended Opportunities")
70
+ st.write(translated_opportunities)
 
 
 
 
 
 
 
 
 
 
 
 
71
  else:
72
+ st.sidebar.error("Please fill in all fields.")
73
 
74
+ # Footer
75
  st.markdown("""
76
  <footer style="text-align:center; padding: 20px; font-size: 1rem; background-color: #f0f0f5;">
77
+ <p>Powered by Groq, Google Translator, and Streamlit</p>
 
 
 
 
 
 
 
78
  <p>Contact: [email protected]</p>
79
  </footer>
80
  """, unsafe_allow_html=True)