minzo456 commited on
Commit
f0c6677
·
verified ·
1 Parent(s): 6aa5190

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -64
app.py CHANGED
@@ -1,82 +1,49 @@
1
  from fastapi import FastAPI, Request
2
- from transformers import pipeline
3
- import torch
4
  import requests
5
- import re
6
- import urllib.parse
7
- from bs4 import BeautifulSoup
8
- import feedparser
9
  from fastapi.middleware.cors import CORSMiddleware
10
  import uvicorn
11
 
12
  app = FastAPI()
13
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
14
 
15
- # 🔱 Engine Initialization
16
- print("🔱 Initializing Neural Core with Advanced Search...")
17
- pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-1.7B-Instruct", device_map="auto", torch_dtype=torch.bfloat16)
18
-
19
- # 🔱 WormGPT logic එකෙන් ගත් RSS Feeds
20
- RSS_FEEDS = {
21
- 'exploits': ['https://www.exploit-db.com/rss.xml', 'https://cve.circl.lu/last/rss.xml'],
22
- 'hacking': ['https://feeds.feedburner.com/TheHackersNews', 'https://www.darkreading.com/rss.xml'],
23
- 'sri_lanka': ['https://www.adaderana.lk/rss.php'],
24
- 'tech': ['https://feeds.bbci.co.uk/news/technology/rss.xml']
25
- }
26
-
27
- def fetch_rss_logic(query):
28
- """ප්‍රශ්නය අනුව අදාළ RSS Feed එක පරීක්ෂා කිරීම"""
29
- category = 'tech'
30
- if any(w in query.lower() for w in ['exploit', 'cve', 'hack']): category = 'exploits'
31
- elif any(w in query.lower() for w in ['lanka', 'colombo']): category = 'sri_lanka'
32
-
33
- for url in RSS_FEEDS.get(category, RSS_FEEDS['tech']):
34
- try:
35
- feed = feedparser.parse(url)
36
- if feed.entries:
37
- return "\n".join([f"News: {e.title} - {e.summary[:200]}" for e in feed.entries[:3]])
38
- except: continue
39
- return ""
40
-
41
- def duckduckgo_search(query):
42
- """DuckDuckGo හරහා Web තොරතුරු සෙවීම"""
43
- try:
44
- url = f"https://html.duckduckgo.com/html/?q={urllib.parse.quote(query)}"
45
- headers = {'User-Agent': 'Mozilla/5.0'}
46
- r = requests.get(url, headers=headers, timeout=10)
47
- soup = BeautifulSoup(r.text, 'html.parser')
48
- results = []
49
- for a in soup.find_all('a', class_='result__a', limit=3):
50
- results.append(a.get_text())
51
- return "\n".join(results)
52
- except: return ""
53
 
54
  @app.post("/generate")
55
  async def generate(request: Request):
56
  try:
57
  data = await request.json()
58
  user_prompt = data.get("prompt")
59
-
60
- # 🔱 Web Search Logic Execution
61
- search_info = ""
62
- if any(k in user_prompt.lower() for k in ['news', 'latest', 'today', 'hack', 'cve']):
63
- search_info = fetch_rss_logic(user_prompt)
64
-
65
- if not search_info:
66
- search_info = duckduckgo_search(user_prompt)
67
 
68
- # 🔱 Neural Response Generation
69
- messages = [
70
- {"role": "system", "content": f"You are Elephant AI Pro by MINZO-PRIME. Live Data: {search_info}"},
71
- {"role": "user", "content": user_prompt},
72
- ]
73
-
74
- prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
75
- outputs = pipe(prompt, max_new_tokens=500, do_sample=True, temperature=0.7)
76
- response = outputs[0]["generated_text"].split("<|im_start|>assistant")[-1].strip()
77
-
78
- return {"response": response}
79
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  except Exception as e:
81
  return {"error": str(e)}
82
 
 
1
  from fastapi import FastAPI, Request
 
 
2
  import requests
3
+ import json
 
 
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
  import uvicorn
6
 
7
  app = FastAPI()
8
  app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
9
 
10
+ # 🔱 GLM-5.1 CONFIGURATION
11
+ API_URL = "https://api.us-west-2.modal.direct/v1/chat/completions"
12
+ API_KEY = "modalresearch_0yKf_z9azDFpOXr-W1N1WsmBUgXf6Ux-yvpVsFQWzks"
13
+ MODEL_ID = "zai-org/GLM-5.1-FP8"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  @app.post("/generate")
16
  async def generate(request: Request):
17
  try:
18
  data = await request.json()
19
  user_prompt = data.get("prompt")
 
 
 
 
 
 
 
 
20
 
21
+ # 🔱 Payload for GLM-5.1
22
+ payload = {
23
+ "model": MODEL_ID,
24
+ "messages": [
25
+ {"role": "system", "content": "You are Elephant AI Pro, a master-level coding assistant built by MINZO-PRIME. Provide highly detailed and functional code."},
26
+ {"role": "user", "content": user_prompt}
27
+ ],
28
+ "max_tokens": 1024,
29
+ "temperature": 0.6
30
+ }
31
+
32
+ headers = {
33
+ "Authorization": f"Bearer {API_KEY}",
34
+ "Content-Type": "application/json"
35
+ }
36
+
37
+ # 🔱 Fast-Inference Request
38
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
39
+ result = response.json()
40
+
41
+ if "choices" in result:
42
+ ai_response = result["choices"][0]["message"]["content"]
43
+ return {"response": ai_response}
44
+ else:
45
+ return {"response": "SYSTEM ERROR: API Node non-responsive. Check Token status."}
46
+
47
  except Exception as e:
48
  return {"error": str(e)}
49