minzo456 commited on
Commit
faefe86
·
verified ·
1 Parent(s): 3807455

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -44
app.py CHANGED
@@ -1,8 +1,7 @@
1
  from fastapi import FastAPI, Request
2
- import requests
3
- import time
4
  from ddgs import DDGS
5
- from datetime import datetime
6
  from fastapi.middleware.cors import CORSMiddleware
7
  import uvicorn
8
 
@@ -15,14 +14,16 @@ app.add_middleware(
15
  allow_headers=["*"],
16
  )
17
 
18
- # 🔱 Engine Upgrade: Llama-3-8B-Instruct (Faster & Stable)
19
- API_URL = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
 
 
20
 
21
  def web_search(query):
22
  try:
23
  with DDGS() as ddgs:
24
- results = [r for r in ddgs.text(query, max_results=3)]
25
- return "\n".join([f"Verified Info: {r['body']}" for r in results])
26
  except: return ""
27
 
28
  @app.post("/generate")
@@ -30,44 +31,18 @@ async def generate(request: Request):
30
  try:
31
  data = await request.json()
32
  user_prompt = data.get("prompt")
33
- search_results = web_search(user_prompt)
34
- current_date = datetime.now().strftime("%Y-%m-%d")
35
 
36
- # 🔱 Optimized Llama-3 Prompt Format
37
- full_prompt = (
38
- f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n"
39
- f"Today is {current_date}. You are Elephant AI Pro, a powerful assistant created by MINZO-PRIME. "
40
- f"Use this web context to provide accurate answers: {search_results}<|eot_id|>"
41
- f"<|start_header_id|>user<|end_header_id|>\n\n{user_prompt}<|eot_id|>"
42
- f"<|start_header_id|>assistant<|end_header_id|>\n\n"
43
- )
44
-
45
- payload = {
46
- "inputs": full_prompt,
47
- "parameters": {
48
- "max_new_tokens": 800,
49
- "temperature": 0.6,
50
- "top_p": 0.9,
51
- "return_full_text": False
52
- }
53
- }
54
-
55
- # 🔱 Fast-Response Logic
56
- for attempt in range(3):
57
- response = requests.post(API_URL, json=payload)
58
- output = response.json()
59
-
60
- # පිළිතුර ලැබුණහොත් වහාම ලබා දෙයි
61
- if isinstance(output, list) and 'generated_text' in output[0]:
62
- return {"response": output[0]['generated_text'].strip()}
63
-
64
- # API එක Load වෙමින් පවතී නම් පමණක් සුළු වෙලාවක් රැඳී සිටියි
65
- if "estimated_time" in str(output):
66
- time.sleep(3)
67
- else:
68
- break
69
-
70
- return {"response": "System Core is ready. Please re-send your command."}
71
 
72
  except Exception as e:
73
  return {"error": str(e)}
 
1
  from fastapi import FastAPI, Request
2
+ from transformers import pipeline
3
+ import torch
4
  from ddgs import DDGS
 
5
  from fastapi.middleware.cors import CORSMiddleware
6
  import uvicorn
7
 
 
14
  allow_headers=["*"],
15
  )
16
 
17
+ # 🔱 Loading local engine (SmolLM2 - Very fast and smart)
18
+ print("🔱 Initializing Internal Neural Core...")
19
+ pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-1.7B-Instruct", device_map="auto")
20
+ print("🔱 Engine Ready!")
21
 
22
  def web_search(query):
23
  try:
24
  with DDGS() as ddgs:
25
+ results = [r for r in ddgs.text(query, max_results=2)]
26
+ return " ".join([r['body'] for r in results])
27
  except: return ""
28
 
29
  @app.post("/generate")
 
31
  try:
32
  data = await request.json()
33
  user_prompt = data.get("prompt")
 
 
34
 
35
+ context = web_search(user_prompt)
36
+
37
+ messages = [
38
+ {"role": "system", "content": f"You are Elephant AI Pro by MINZO-PRIME. Web Data: {context}"},
39
+ {"role": "user", "content": user_prompt},
40
+ ]
41
+
42
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
43
+ outputs = pipe(prompt, max_new_tokens=500, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
44
+
45
+ return {"response": outputs[0]["generated_text"].split("<|im_start|>assistant")[-1].strip()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  except Exception as e:
48
  return {"error": str(e)}