Spaces:
Configuration error
Configuration error
File size: 2,052 Bytes
98bc1c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import json
from groq import Groq
import os
class VariableExtractor:
def __init__(self):
self.client = None
def set_api_key(self, api_key):
"""Set Groq API key"""
self.client = Groq(api_key=api_key)
def extract_variables(self, business_problem):
"""Extract relevant variables from business problem description"""
if not self.client:
# Fallback to mock data if no API key
return self._get_mock_variables()
try:
system_prompt = """You are an expert business analyst. Extract relevant variables for marketing analysis from the given business problem. Return only a JSON array of variable names, nothing else."""
user_prompt = f"""Business Problem: {business_problem}
Extract 6-10 relevant variables that would be important for analyzing this marketing/business problem. Focus on measurable, actionable variables.
Return format: ["Variable 1", "Variable 2", "Variable 3", ...]"""
completion = self.client.chat.completions.create(
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
model="llama-3.1-70b-versatile",
temperature=0.7,
max_tokens=1024
)
response = completion.choices[0].message.content.strip()
variables = json.loads(response)
return variables
except Exception as e:
print(f"Error extracting variables: {e}")
return self._get_mock_variables()
def _get_mock_variables(self):
"""Fallback mock variables"""
return [
"Customer Age",
"Purchase Amount",
"Product Category",
"Marketing Channel",
"Customer Location",
"Purchase Frequency",
"Customer Satisfaction Score",
"Time to Purchase"
] |