saherPervaiz commited on
Commit
4cad79d
·
verified ·
1 Parent(s): 2038bb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -1,22 +1,35 @@
1
  import os
 
2
  import streamlit as st
3
- from groq import Groq
4
  from googletrans import Translator
5
  import asyncio
6
 
7
- # Initialize the Groq client
8
- client = Groq(api_key="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")
 
 
 
 
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):
@@ -78,10 +91,13 @@ if st.button("Send"):
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})
 
1
  import os
2
+ import requests
3
  import streamlit as st
 
4
  from googletrans import Translator
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):
 
91
  with st.spinner("Fetching opportunities..."):
92
  try:
93
  # Get the opportunity details based on user query
94
+ opportunities_response = get_opportunities(user_query)
95
+
96
+ # Extract the response content, modify based on actual response format
97
+ opportunities_text = opportunities_response.get("choices", [{}])[0].get("message", {}).get("content", "No data found.")
98
 
99
  # Run the async translate function and get the translated text
100
+ translated_opportunities = asyncio.run(translate_text(opportunities_text, languages[selected_language]))
101
 
102
  # Append AI response to chat history
103
  st.session_state.messages.append({"role": "ai", "content": translated_opportunities})