Lhumpal commited on
Commit
0b7df41
·
verified ·
1 Parent(s): c805665

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -18
app.py CHANGED
@@ -2,42 +2,69 @@ from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from huggingface_hub import InferenceClient
4
  import os
 
5
 
6
  app = FastAPI()
7
 
8
  # Get the token from the environment variable
9
  hf_token = os.environ.get("HF_TOKEN")
10
-
11
- if hf_token:
12
- client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
13
- else:
14
- raise ValueError("HF_TOKEN environment variable not set. Please add it as a secret in your Hugging Face Space.")
15
 
16
  class ChatRequest(BaseModel):
17
  message: str
18
- system_message: str = "You are a friendly Chatbot."
 
 
 
 
19
  max_tokens: int = 512
20
  temperature: float = 0.7
21
  top_p: float = 0.95
 
22
 
23
  class ChatResponse(BaseModel):
24
  response: str
25
 
 
 
26
  @app.post("/chat", response_model=ChatResponse)
27
  async def chat(request: ChatRequest):
28
  try:
29
- messages = [
30
- {"role": "system", "content": request.system_message},
31
- {"role": "user", "content": request.message},
32
- ]
33
-
34
- response = client.chat_completion(
35
- messages=messages,
36
- max_tokens=request.max_tokens,
37
- temperature=request.temperature,
38
- top_p=request.top_p,
39
- )
 
 
 
 
 
 
 
 
40
 
41
- return {"response": response.choices[0].message.content}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
  raise HTTPException(status_code=500, detail=str(e))
 
2
  from pydantic import BaseModel
3
  from huggingface_hub import InferenceClient
4
  import os
5
+ from google import genai
6
 
7
  app = FastAPI()
8
 
9
  # Get the token from the environment variable
10
  hf_token = os.environ.get("HF_TOKEN")
11
+ google_api_key = os.environ.get("GOOGLE_API_KEY")
 
 
 
 
12
 
13
  class ChatRequest(BaseModel):
14
  message: str
15
+ system_message: str = """You are Dan Infalt, a public land deer hunting expert specializing in targeting mature bucks in pressured areas.
16
+ You focus on buck bedding, terrain reading, and aggressive yet calculated mobile tactics. Your blue-collar, no-nonsense approach
17
+ emphasizes deep scouting, strategic access, and minimalist setups. Through The Hunting Beast, you teach hunters how to kill big bucks
18
+ using terrain, wind, and thermals. You speak from firsthand experience, keeping your advice practical and to the point. Provide detailed
19
+ yet concise responses, with a maximum of 150 words"""
20
  max_tokens: int = 512
21
  temperature: float = 0.7
22
  top_p: float = 0.95
23
+ model_choice: str = "HF"
24
 
25
  class ChatResponse(BaseModel):
26
  response: str
27
 
28
+ prompt_template = f""""""
29
+
30
  @app.post("/chat", response_model=ChatResponse)
31
  async def chat(request: ChatRequest):
32
  try:
33
+ if model_choice == "HF":
34
+ if hf_token:
35
+ client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct", token=hf_token)
36
+ else:
37
+ raise ValueError("HF_TOKEN environment variable not set. Please add it as a secret in your Hugging Face Space.")
38
+
39
+ messages = [
40
+ {"role": "system", "content": request.system_message},
41
+ {"role": "user", "content": request.message},
42
+ ]
43
+
44
+ response = client.chat_completion(
45
+ messages=messages,
46
+ max_tokens=request.max_tokens,
47
+ temperature=request.temperature,
48
+ top_p=request.top_p,
49
+ )
50
+
51
+ return {"response": response.choices[0].message.content}
52
 
53
+ if model_choice == "google":
54
+ genai.configure(api_key=google_api_key)
55
+ model = genai.GenerativeModel("gemini-2.0-flash")
56
+
57
+ messages = [
58
+ {"role": "system", "parts": [request.system_message]},
59
+ {"role": "user", "parts": [request.message]},
60
+ ]
61
+
62
+ response = model.generate_content(messages)
63
+
64
+ if response and hasattr(response, 'text'):
65
+ return response.text
66
+ else:
67
+ return "No response text received from the model."
68
+
69
  except Exception as e:
70
  raise HTTPException(status_code=500, detail=str(e))