Nick088 commited on
Commit
9ab5f9b
·
verified ·
1 Parent(s): c5c665e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import groq
 
2
  import gradio as gr
3
  import re
4
  import json
@@ -6,27 +7,30 @@ import os
6
 
7
 
8
  # Setup Groq
9
- client = groq.Client(api_key=os.environ.get("GROQ_API_KEY")) # Correct initialization
10
  model = "llama-3.3-70b-versatile"
11
 
12
  def handle_groq_error(e, model_name):
13
  error_data = e.args[0]
14
-
15
  if isinstance(error_data, str):
16
- # Use regex to extract the JSON part of the string
17
  json_match = re.search(r'(\{.*\})', error_data)
18
  if json_match:
19
  json_str = json_match.group(1)
20
- # Ensure the JSON string is well-formed
21
- json_str = json_str.replace("'", '"') # Replace single quotes with double quotes
22
  error_data = json.loads(json_str)
23
 
24
- if isinstance(e, groq.RateLimitError):
 
 
 
 
25
  if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
26
  error_message = error_data['error']['message']
27
- raise gr.Error(error_message)
 
28
  else:
29
- raise gr.Error(f"Error during Groq API call: {e}")
30
 
31
 
32
 
@@ -42,12 +46,17 @@ def calculate_and_roast(expression):
42
  # Groq API call
43
  try:
44
  prompt = f"Roast me for calculating something as simple as '{expression}'. Make it funny."
45
- response = client.request(model, {"prompt": prompt})
46
- roast = response["text"][0]
 
 
 
47
  return result, roast
48
 
49
- except groq.GroqApiException as e:
50
- handle_groq_error(e, model)
 
 
51
 
52
  except (ValueError, TypeError, SyntaxError, ZeroDivisionError) as e:
53
  return f"Calculation error: {e}", None
 
1
  import groq
2
+ from groq import Groq, AuthenticationError, RateLimitError
3
  import gradio as gr
4
  import re
5
  import json
 
7
 
8
 
9
  # Setup Groq
10
+ client = groq.Client(api_key=os.environ.get("GROQ_API_KEY"))
11
  model = "llama-3.3-70b-versatile"
12
 
13
  def handle_groq_error(e, model_name):
14
  error_data = e.args[0]
15
+
16
  if isinstance(error_data, str):
 
17
  json_match = re.search(r'(\{.*\})', error_data)
18
  if json_match:
19
  json_str = json_match.group(1)
20
+ json_str = json_str.replace("'", '"')
 
21
  error_data = json.loads(json_str)
22
 
23
+ if isinstance(e, AuthenticationError):
24
+ if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
25
+ error_message = error_data['error']['message']
26
+ raise discord.app_commands.AppCommandError(error_message)
27
+ elif isinstance(e, RateLimitError):
28
  if isinstance(error_data, dict) and 'error' in error_data and 'message' in error_data['error']:
29
  error_message = error_data['error']['message']
30
+ error_message = re.sub(r'org_[a-zA-Z0-9]+', 'org_(censored)', error_message)
31
+ raise discord.app_commands.AppCommandError(error_message)
32
  else:
33
+ raise discord.app_commands.AppCommandError(f"Error during Groq API call: {e}")
34
 
35
 
36
 
 
46
  # Groq API call
47
  try:
48
  prompt = f"Roast me for calculating something as simple as '{expression}'. Make it funny."
49
+ chat_completion = client.chat.completions.create(
50
+ messages=[{"role": "user", "content": prompt}],
51
+ model=model
52
+ )
53
+ roast = chat_completion.choices[0].message.content
54
  return result, roast
55
 
56
+ except AuthenticationError as e:
57
+ handle_groq_error(e, model)
58
+ except RateLimitError as e:
59
+ handle_groq_error(e, model)
60
 
61
  except (ValueError, TypeError, SyntaxError, ZeroDivisionError) as e:
62
  return f"Calculation error: {e}", None