File size: 1,754 Bytes
915b06b
ddaad63
634a8f0
915b06b
634a8f0
915b06b
 
634a8f0
ddaad63
 
 
 
 
 
 
 
 
 
 
915b06b
 
 
 
 
 
 
 
 
 
634a8f0
ddaad63
634a8f0
ddaad63
 
 
 
 
 
 
 
915b06b
 
 
 
 
 
 
 
 
 
 
 
 
 
ddaad63
 
 
915b06b
634a8f0
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
58
59
60
# intent_parser.py - Smart fallback to Gemini or OpenAI for intent classification

import os
import json

USE_GEMINI = bool(os.getenv("GEMINI_API_KEY"))
USE_OPENAI = bool(os.getenv("OPENAI_API_KEY"))

INTENT_CATEGORIES = [
    "educational",
    "assistive",
    "entertainment",
    "industrial",
    "home automation",
    "healthcare",
    "retail",
    "creative"
]

if USE_GEMINI:
    import google.generativeai as genai
    genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
    gemini_model = genai.GenerativeModel(model_name="models/gemini-1.5-pro-latest")

elif USE_OPENAI:
    from openai import OpenAI
    openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
else:
    raise EnvironmentError("No valid API key set. Please set GEMINI_API_KEY or OPENAI_API_KEY.")

def classify_robot_idea(user_input: str) -> str:
    prompt = f"""
    Classify this user idea into one of the following categories:
    {', '.join(INTENT_CATEGORIES)}.
    Only return the category word. If none fits, return 'creative'.

    Idea: {user_input}
    Category:
    """

    if USE_GEMINI:
        response = gemini_model.generate_content(prompt)
        return response.text.strip().lower()

    elif USE_OPENAI:
        response = openai_client.chat.completions.create(
            model="gpt-4o",
            messages=[
                {"role": "system", "content": "You are a classification AI for robotics ideas."},
                {"role": "user", "content": prompt}
            ],
            temperature=0
        )
        return response.choices[0].message.content.strip().lower()

# Example
if __name__ == "__main__":
    idea = "Build a robot that helps kids focus while doing homework."
    print("Predicted Intent:", classify_robot_idea(idea))