File size: 3,400 Bytes
2d8aa41
 
 
 
8408778
8378f3d
8408778
2d8aa41
ca29edb
8408778
 
 
 
 
2d8aa41
8378f3d
2d8aa41
 
8408778
 
 
 
 
 
 
 
 
 
 
2d8aa41
 
8408778
2d8aa41
 
0e59f48
8408778
 
2d8aa41
8408778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d8aa41
8408778
 
 
 
 
 
 
 
 
 
8378f3d
8408778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d8aa41
8408778
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from huggingface_hub import InferenceClient
import os

def query_model(prompt):
    """
    Query the Qwen2.5-7B-Instruct model with the given prompt
    """
    try:
        HF_TOKEN = os.getenv("HF_TOKEN")
        
        if not HF_TOKEN:
            return "Error: HF_TOKEN not found. Please set your Hugging Face token in environment variables."
        
        # Initialize the client
        client = InferenceClient(
            model="Qwen/Qwen2.5-7B-Instruct",
            token=HF_TOKEN
        )
        
        # Enhanced system prompt for better responses
        system_prompt = """You are a certified professional fitness trainer with expertise in creating personalized workout plans. 
        Always provide complete, detailed workout plans with:
        - Clear day-by-day structure
        - Specific exercises with sets, reps, and rest periods
        - Warm-up and cool-down recommendations
        - Safety considerations based on user's profile
        When asked for a 5-day plan, ensure ALL 5 days are included with clear day headers."""
        
        # Make the API call
        response = client.chat_completion(
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt}
            ],
            max_tokens=3000,  # Increased for complete 5-day plan
            temperature=0.7,
            top_p=0.95
        )
        
        # Extract and return the response
        workout_plan = response.choices[0].message.content
        
        # Verify if the response contains all 5 days
        days_found = sum([f"Day {i}" in workout_plan for i in range(1, 6)])
        
        if days_found < 5:
            # If incomplete, try one more time with more explicit instruction
            retry_prompt = prompt + "\n\nIMPORTANT: The previous response was incomplete. Please ensure ALL 5 days (Day 1 through Day 5) are included in the plan. Each day should be clearly marked with 'Day X' header and include 4-6 exercises."
            
            retry_response = client.chat_completion(
                messages=[
                    {"role": "system", "content": system_prompt},
                    {"role": "user", "content": retry_prompt}
                ],
                max_tokens=2500,
                temperature=0.7
            )
            workout_plan = retry_response.choices[0].message.content
        
        return workout_plan
        
    except Exception as e:
        return f"Error generating workout plan: {str(e)}"

def test_api_connection():
    """
    Test function to verify API connection
    """
    try:
        HF_TOKEN = os.getenv("HF_TOKEN")
        if not HF_TOKEN:
            return False, "HF_TOKEN not found"
        
        client = InferenceClient(
            model="Qwen/Qwen2.5-7B-Instruct",
            token=HF_TOKEN
        )
        
        # Simple test prompt
        response = client.chat_completion(
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": "Say 'API connection successful' if you can read this."}
            ],
            max_tokens=50,
            temperature=0.1
        )
        
        return True, "API connection successful"
        
    except Exception as e:
        return False, f"API connection failed: {str(e)}"