saherPervaiz commited on
Commit
dd70a62
·
verified ·
1 Parent(s): 6026b89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -4,27 +4,19 @@ 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_query):
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 query '{user_query}', find scholarships, internships, online courses, and career advice suitable for them, including deadlines for application and links to apply."
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):
@@ -84,14 +76,17 @@ if st.button("Send"):
84
  st.session_state.messages.append({"role": "user", "content": user_query})
85
 
86
  with st.spinner("Fetching opportunities..."):
87
- # Get the opportunity details based on user query
88
- opportunities = get_opportunities(user_query)
89
-
90
- # Run the async translate function and get the translated text
91
- translated_opportunities = asyncio.run(translate_text(opportunities, languages[selected_language]))
92
-
93
- # Append AI response to chat history
94
- st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
 
 
 
95
 
96
  # Scroll to the latest message
97
  st.experimental_rerun()
 
4
  from googletrans import Translator
5
  import asyncio
6
 
7
+ # Initialize the Groq client
8
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
9
+
10
+ # Function to get recommendations from Groq API based on user input
11
  def get_opportunities(user_query):
12
+ # Create a chat completion request
13
+ chat_completion = client.chat.completions.create(
14
+ messages=[{"role": "user", "content": user_query}],
15
+ model="llama-3.3-70b-versatile"
 
 
 
 
 
 
 
 
 
 
 
 
16
  )
17
 
18
+ # Return the response content
19
+ return chat_completion.choices[0].message.content
20
 
21
  # Function to translate text into the selected language (async version)
22
  async def translate_text(text, target_language):
 
76
  st.session_state.messages.append({"role": "user", "content": user_query})
77
 
78
  with st.spinner("Fetching opportunities..."):
79
+ try:
80
+ # Get the opportunity details based on user query
81
+ opportunities = get_opportunities(user_query)
82
+
83
+ # Run the async translate function and get the translated text
84
+ translated_opportunities = asyncio.run(translate_text(opportunities, languages[selected_language]))
85
+
86
+ # Append AI response to chat history
87
+ st.session_state.messages.append({"role": "ai", "content": translated_opportunities})
88
+ except Exception as e:
89
+ st.error(f"Error: {e}")
90
 
91
  # Scroll to the latest message
92
  st.experimental_rerun()