MINZO4546 commited on
Commit
b0ae281
·
verified ·
1 Parent(s): 4803462

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -1,14 +1,22 @@
1
  import torch
 
2
  import requests
3
  from fastapi import FastAPI
4
  from transformers import pipeline
5
 
 
 
 
 
 
 
6
  main = FastAPI()
7
 
8
- # 🔱 Inachi Core Configuration
9
  MODEL_ID = "google/gemma-3-1b-it"
10
 
11
- # Load the model with CPU optimizations
 
12
  pipe = pipeline(
13
  "text-generation",
14
  model=MODEL_ID,
@@ -17,35 +25,35 @@ pipe = pipeline(
17
  trust_remote_code=True
18
  )
19
 
20
- # 🔱 Simple Web Search Tool (DuckDuckGo API - No Key Required)
21
  def web_search(query):
22
  try:
 
23
  url = f"https://api.duckduckgo.com/?q={query}&format=json"
24
- response = requests.get(url).json()
25
- return response.get("AbstractText", "No specific web data found.")
26
  except:
27
- return "Search failed."
28
 
29
  @main.post("/v1/chat")
30
  async def chat(data: dict):
31
  user_query = data.get("message", "")
32
 
33
- # 🔱 Inachi Identity & System Instruction
34
- # මෙතනින් තමයි එයාට තමන් කවුද කියලා කියලා දෙන්නේ
35
  system_prompt = (
36
  "You are Inachi AI, a highly advanced assistant developed by the Inachi Team. "
37
- "Your goal is to provide technical, precise, and helpful information. "
38
- "Always identify yourself as Inachi AI when asked."
39
  )
40
 
41
- # Web search check (සරලව search කරන්න ඕනෙද කියලා බලනවා)
42
  search_context = ""
43
- if "search" in user_query.lower() or "latest" in user_query.lower():
44
- search_context = f"\nWeb Search Result: {web_search(user_query)}"
45
 
46
- # Prompt එක සැකසීම
47
- full_prompt = f"{system_prompt}\nContext: {search_context}\nUser: {user_query}\nInachi AI:"
48
 
 
49
  results = pipe(
50
  full_prompt,
51
  max_new_tokens=512,
@@ -54,10 +62,10 @@ async def chat(data: dict):
54
  top_p=0.9
55
  )
56
 
57
- # පිරිසිදු පිළිතුර ලබා ගැනීම
58
  reply = results[0]['generated_text'].split("Inachi AI:")[-1].strip()
59
  return {"reply": reply}
60
 
61
  if __name__ == "__main__":
62
  import uvicorn
 
63
  uvicorn.run(main, host="0.0.0.0", port=7860)
 
1
  import torch
2
+ import os
3
  import requests
4
  from fastapi import FastAPI
5
  from transformers import pipeline
6
 
7
+ # 🔱 CPU Core Management: Stop 99% CPU Usage
8
+ # HF Free Space එකක සාමාන්‍යයෙන් CPU Cores 2ක් තියෙන නිසා අපි 2කට සීමා කරමු
9
+ os.environ["OMP_NUM_THREADS"] = "2"
10
+ os.environ["MKL_NUM_THREADS"] = "2"
11
+ torch.set_num_threads(2)
12
+
13
  main = FastAPI()
14
 
15
+ # 🔱 Inachi Identity Settings
16
  MODEL_ID = "google/gemma-3-1b-it"
17
 
18
+ # 🔱 Optimized Pipeline
19
+ # bfloat16 පාවිච්චි කිරීමෙන් RAM සහ CPU මතකය ඉතිරි වේ
20
  pipe = pipeline(
21
  "text-generation",
22
  model=MODEL_ID,
 
25
  trust_remote_code=True
26
  )
27
 
 
28
  def web_search(query):
29
  try:
30
+ # Simple DuckDuckGo API for search context
31
  url = f"https://api.duckduckgo.com/?q={query}&format=json"
32
+ response = requests.get(url, timeout=5).json()
33
+ return response.get("AbstractText", "No specific data found.")
34
  except:
35
+ return "Search unavailable."
36
 
37
  @main.post("/v1/chat")
38
  async def chat(data: dict):
39
  user_query = data.get("message", "")
40
 
41
+ # 🔱 System Identity: Developed by Inachi Team
 
42
  system_prompt = (
43
  "You are Inachi AI, a highly advanced assistant developed by the Inachi Team. "
44
+ "You are an expert in system architecture and web development. "
45
+ "Always identify as Inachi AI."
46
  )
47
 
48
+ # Search logic
49
  search_context = ""
50
+ if "search" in user_query.lower():
51
+ search_context = f"\nWeb Context: {web_search(user_query)}"
52
 
53
+ # Prompt construction
54
+ full_prompt = f"{system_prompt}\n{search_context}\nUser: {user_query}\nInachi AI:"
55
 
56
+ # 🔱 Inference with limited tokens for speed
57
  results = pipe(
58
  full_prompt,
59
  max_new_tokens=512,
 
62
  top_p=0.9
63
  )
64
 
 
65
  reply = results[0]['generated_text'].split("Inachi AI:")[-1].strip()
66
  return {"reply": reply}
67
 
68
  if __name__ == "__main__":
69
  import uvicorn
70
+ # HF Spaces uses port 7860 by default
71
  uvicorn.run(main, host="0.0.0.0", port=7860)