dawit45 commited on
Commit
86a7dd6
·
verified ·
1 Parent(s): b32688f

Update utils/ai_engine.py

Browse files
Files changed (1) hide show
  1. utils/ai_engine.py +13 -16
utils/ai_engine.py CHANGED
@@ -3,19 +3,17 @@ from groq import Groq
3
  from openai import OpenAI
4
 
5
  # -----------------------------------------------------------
6
- # 🔑 Load API Keys (HuggingFace Secrets)
7
  # -----------------------------------------------------------
8
 
9
  GROQ_KEY = os.getenv("GROQ_API_KEY")
10
  OPENAI_KEY = os.getenv("OPENAI_API_KEY")
11
 
12
  # -----------------------------------------------------------
13
- # 🚀 Initialize Clients (safely)
14
  # -----------------------------------------------------------
15
 
16
  groq_client = Groq(api_key=GROQ_KEY) if GROQ_KEY else None
17
-
18
- # ⚠️ Disable OpenAI automatically if key exists but quota is empty
19
  openai_client = OpenAI(api_key=OPENAI_KEY) if OPENAI_KEY else None
20
 
21
 
@@ -26,27 +24,27 @@ openai_client = OpenAI(api_key=OPENAI_KEY) if OPENAI_KEY else None
26
  def generate_text(prompt: str) -> str:
27
  """
28
  AI text generation:
29
- 1️⃣ Groq (primary, free, fast)
30
- 2️⃣ OpenAI (fallback ONLY if available)
31
  """
32
 
33
  # ----------------------------
34
- # 1️⃣ GROQ (PRIMARY)
35
  # ----------------------------
36
  if groq_client:
37
  try:
38
  resp = groq_client.chat.completions.create(
39
- model="llama-3.1-8b-instant",
40
- messages=[{"role": "user", "content": prompt}],
41
- max_tokens=700,
42
- temperature=0.3
43
- )
44
- return resp.choices[0].message.content
45
  except Exception as e:
46
  return f"🚫 Groq failed: {e}"
47
 
48
  # ----------------------------
49
- # 2️⃣ OPENAI (OPTIONAL FALLBACK)
50
  # ----------------------------
51
  if openai_client:
52
  try:
@@ -57,11 +55,10 @@ return resp.choices[0].message.content
57
  temperature=0.3
58
  )
59
  return resp.choices[0].message.content
60
-
61
  except Exception as e:
62
  return f"🚫 OpenAI failed: {e}"
63
 
64
  # ----------------------------
65
  # 3️⃣ NO KEYS
66
  # ----------------------------
67
- return "❌ No AI keys found. Please add GROQ_API_KEY in HuggingFace Secrets."
 
3
  from openai import OpenAI
4
 
5
  # -----------------------------------------------------------
6
+ # 🔑 Load API Keys
7
  # -----------------------------------------------------------
8
 
9
  GROQ_KEY = os.getenv("GROQ_API_KEY")
10
  OPENAI_KEY = os.getenv("OPENAI_API_KEY")
11
 
12
  # -----------------------------------------------------------
13
+ # 🚀 Initialize Clients
14
  # -----------------------------------------------------------
15
 
16
  groq_client = Groq(api_key=GROQ_KEY) if GROQ_KEY else None
 
 
17
  openai_client = OpenAI(api_key=OPENAI_KEY) if OPENAI_KEY else None
18
 
19
 
 
24
  def generate_text(prompt: str) -> str:
25
  """
26
  AI text generation:
27
+ 1️⃣ Groq (primary)
28
+ 2️⃣ OpenAI (fallback)
29
  """
30
 
31
  # ----------------------------
32
+ # 1️⃣ GROQ
33
  # ----------------------------
34
  if groq_client:
35
  try:
36
  resp = groq_client.chat.completions.create(
37
+ model="llama-3.1-8b-instant",
38
+ messages=[{"role": "user", "content": prompt}],
39
+ max_tokens=700,
40
+ temperature=0.3
41
+ )
42
+ return resp.choices[0].message.content
43
  except Exception as e:
44
  return f"🚫 Groq failed: {e}"
45
 
46
  # ----------------------------
47
+ # 2️⃣ OPENAI (fallback)
48
  # ----------------------------
49
  if openai_client:
50
  try:
 
55
  temperature=0.3
56
  )
57
  return resp.choices[0].message.content
 
58
  except Exception as e:
59
  return f"🚫 OpenAI failed: {e}"
60
 
61
  # ----------------------------
62
  # 3️⃣ NO KEYS
63
  # ----------------------------
64
+ return "❌ No AI keys found. Please add GROQ_API_KEY to HuggingFace Secrets."