saherPervaiz commited on
Commit
4874a99
·
verified ·
1 Parent(s): 65f2173

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -63
app.py CHANGED
@@ -1,80 +1,69 @@
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)
 
1
  import os
2
  import streamlit as st
3
+ from groq import Groq
 
4
 
5
+ # Initialize Groq client
6
+ API_KEY = os.getenv("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") # Ensure GROQ_API_KEY is set in your environment variables
7
  if not API_KEY:
8
  st.error("API key is missing. Please set the GROQ_API_KEY environment variable.")
9
  st.stop()
10
 
11
+ client = Groq(api_key=API_KEY)
 
12
 
13
+ # Streamlit app configuration
14
+ st.set_page_config(page_title="AI Chatbot", page_icon="🤖", layout="wide")
15
+ st.title("AI-Powered Chatbot with Groq")
16
 
17
+ # Sidebar for user input
18
+ st.sidebar.header("Chatbot Settings")
19
+ chat_model = st.sidebar.selectbox("Select AI Model", ["llama-3.3-70b-versatile", "llama-2.0-50b-creative"])
 
 
20
 
21
+ # Chat interface
22
+ st.write("## Chat with AI")
23
+ if "messages" not in st.session_state:
24
+ st.session_state.messages = []
 
 
 
 
 
 
 
 
25
 
26
+ # Display conversation history
27
+ for msg in st.session_state.messages:
28
+ if msg["role"] == "user":
29
+ st.write(f"**You:** {msg['content']}")
30
+ else:
31
+ st.write(f"**AI:** {msg['content']}")
32
+
33
+ # User input for the chatbot
34
+ user_input = st.text_input("Enter your message:", key="user_input")
 
 
 
 
 
 
35
 
36
+ # On submit, process the input
37
+ if st.button("Send"):
38
+ if user_input:
39
+ # Add user input to the chat history
40
+ st.session_state.messages.append({"role": "user", "content": user_input})
41
+
42
+ # Call Groq API
43
+ with st.spinner("Thinking..."):
44
+ try:
45
+ chat_completion = client.chat.completions.create(
46
+ messages=st.session_state.messages,
47
+ model=chat_model,
48
+ )
49
+ ai_response = chat_completion.choices[0].message.content
50
 
51
+ # Add AI response to the chat history
52
+ st.session_state.messages.append({"role": "assistant", "content": ai_response})
53
+
54
+ # Display the AI's response
55
+ st.write(f"**AI:** {ai_response}")
56
+ except Exception as e:
57
+ st.error(f"Error: {e}")
 
 
58
  else:
59
+ st.warning("Please enter a message before sending.")
60
+
61
+ # Clear conversation button
62
+ if st.button("Clear Conversation"):
63
+ st.session_state.messages = []
64
 
65
  # Footer
66
  st.markdown("""
67
+ ---
68
+ Powered by [Groq](https://groq.com) | Deployed on [Hugging Face Spaces](https://huggingface.co/spaces)
69
+ """)