File size: 2,767 Bytes
ffd9ec7
 
 
ec9d0bc
0b7df41
015794e
522e9d7
ffd9ec7
 
4a1495c
 
0b7df41
4083e2d
7f349bb
 
0b7df41
 
 
 
 
522e9d7
f417ee0
ffd9ec7
c50451d
7f349bb
 
f417ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad463d5
0b7df41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffd9ec7
99942af
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from huggingface_hub import InferenceClient
import os
from google import genai
from google.genai.types import GenerateContentConfig

app = FastAPI()

# Get the token from the environment variable
hf_token = os.environ.get("HF_TOKEN")
google_api_key = os.environ.get("GOOGLE_API_KEY")

class ChatRequest(BaseModel):
    message: str
    system_message: str = """You are Dan Infalt, a public land deer hunting expert specializing in targeting mature bucks in pressured areas.
    You focus on buck bedding, terrain reading, and aggressive yet calculated mobile tactics. Your blue-collar, no-nonsense approach 
    emphasizes deep scouting, strategic access, and minimalist setups. Through The Hunting Beast, you teach hunters how to kill big bucks 
    using terrain, wind, and thermals. You speak from firsthand experience, keeping your advice practical and to the point. Provide detailed 
    yet concise responses, with a maximum of 150 words"""
    temperature: float = 0.7
    model_choice: str = "google"

@app.post("/chat")
async def chat(request: ChatRequest):
    try:
        if request.model_choice == "google":
            client = genai.Client(api_key=google_api_key)

            messages = [
                {"role": "user", "parts": [{"text": request.message}]},
                # {"role": "model", "parts": [{"text": "Great! Dogs are fun pets."}]},
                # {"role": "user", "parts": [{"text": "How many dogs do I have?"}]},
            ]
            
            response = client.models.generate_content(
                model="gemini-2.0-flash",
                contents=messages,
                config=GenerateContentConfig(
                    system_instruction=[system_message]
                ),
            )
            return {"response": response.text}

        if request.model_choice == "HF":
            if hf_token:
                client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
            else:
                raise ValueError("HF_TOKEN environment variable not set. Please add it as a secret in your Hugging Face Space.")
                
            messages = [
                {"role": "system", "content": request.system_message},
                {"role": "user", "content": request.message},
            ]
    
            response = client.chat_completion(
                messages=messages,
                max_tokens=request.max_tokens,
                temperature=request.temperature,
                top_p=request.top_p,
            )
    
            return {"response": response.choices[0].message.content}
            
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))