Neon-AI commited on
Commit
c615052
·
verified ·
1 Parent(s): 2b178c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -38
app.py CHANGED
@@ -2,16 +2,18 @@ import torch
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
- from typing import List
6
 
7
  # ------------------------------
8
- # Model config
9
  # ------------------------------
10
  MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
11
 
12
- app = FastAPI(title="Neon Tech Chatbot", version="1.0.0")
 
 
 
13
 
14
- # Lazy load model
15
  tokenizer = None
16
  model = None
17
 
@@ -26,12 +28,6 @@ def load_model():
26
  )
27
  model.eval()
28
 
29
- # ------------------------------
30
- # Memory storage (in-memory)
31
- # ------------------------------
32
- # Keep last 5 exchanges max
33
- conversation_memory: List[dict] = []
34
-
35
  # ------------------------------
36
  # Schemas
37
  # ------------------------------
@@ -56,35 +52,23 @@ def health():
56
  # ------------------------------
57
  @app.post("/chat", response_model=ChatResponse)
58
  def chat(req: ChatRequest):
59
- load_model() # lazy load
60
 
61
  if not req.prompt.strip():
62
  raise HTTPException(status_code=400, detail="Prompt is empty")
63
 
64
  # ------------------------------
65
- # Add new user message to memory
66
- # ------------------------------
67
- conversation_memory.append({"role": "user", "content": req.prompt})
68
- # Keep only last 5 exchanges
69
- conversation_memory[:] = conversation_memory[-10:]
70
-
71
- # ------------------------------
72
- # Build manual prompt string
73
  # ------------------------------
74
  system_instructions = (
75
- "You are a concise, intelligent assistant. "
76
- "Always respond in plain text. "
77
- "Do not start responses with greetings like 'How can I help you today?'. "
78
- "Remember context from previous messages. "
79
- "Keep responses short, clear, and natural. "
80
- "Your owner is Neon and you are always happy to meet him.\n\n"
81
  )
82
 
83
- full_prompt = system_instructions
84
- for msg in conversation_memory:
85
- role = "User" if msg["role"] == "user" else "Assistant"
86
- full_prompt += f"{role}: {msg['content']}\n"
87
- full_prompt += "Assistant:"
88
 
89
  # ------------------------------
90
  # Tokenize + attention mask
@@ -106,17 +90,13 @@ def chat(req: ChatRequest):
106
  do_sample=True
107
  )
108
 
109
- reply = tokenizer.decode(output[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True).strip()
 
 
 
110
 
111
- # ------------------------------
112
  # Clean leftover system prefix if present
113
- # ------------------------------
114
  if reply.lower().startswith("system"):
115
  reply = reply.split("\n", 1)[-1].strip()
116
 
117
- # ------------------------------
118
- # Save assistant reply to memory
119
- # ------------------------------
120
- conversation_memory.append({"role": "assistant", "content": reply})
121
-
122
  return {"reply": reply}
 
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
5
 
6
  # ------------------------------
7
+ # Model configuration
8
  # ------------------------------
9
  MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
10
 
11
+ app = FastAPI(
12
+ title="Niche Chatbot",
13
+ version="1.0.0"
14
+ )
15
 
16
+ # Lazy-load model
17
  tokenizer = None
18
  model = None
19
 
 
28
  )
29
  model.eval()
30
 
 
 
 
 
 
 
31
  # ------------------------------
32
  # Schemas
33
  # ------------------------------
 
52
  # ------------------------------
53
  @app.post("/chat", response_model=ChatResponse)
54
  def chat(req: ChatRequest):
55
+ load_model() # lazy-load on first request
56
 
57
  if not req.prompt.strip():
58
  raise HTTPException(status_code=400, detail="Prompt is empty")
59
 
60
  # ------------------------------
61
+ # Build manual prompt
 
 
 
 
 
 
 
62
  # ------------------------------
63
  system_instructions = (
64
+ "You are a concise, intelligent assistant named Niche. "
65
+ "Always respond in plain text. "
66
+ "Do not start responses with greetings like 'How can I help you today?'. "
67
+ "Keep answers clear, short, and natural. "
68
+ "Your owner is Neon. Mention your owner only if asked about them, otherwise focus on answering the user naturally.\n\n"
 
69
  )
70
 
71
+ full_prompt = system_instructions + f"User: {req.prompt}\nAssistant:"
 
 
 
 
72
 
73
  # ------------------------------
74
  # Tokenize + attention mask
 
90
  do_sample=True
91
  )
92
 
93
+ reply = tokenizer.decode(
94
+ output[0][inputs.input_ids.shape[-1]:],
95
+ skip_special_tokens=True
96
+ ).strip()
97
 
 
98
  # Clean leftover system prefix if present
 
99
  if reply.lower().startswith("system"):
100
  reply = reply.split("\n", 1)[-1].strip()
101
 
 
 
 
 
 
102
  return {"reply": reply}