File size: 7,075 Bytes
2dc4fb9 a971d1c c51f8c4 e4a835d 3683c2c f817cfc 4b20d59 a971d1c 7b37201 12db0e1 e28d45d 2dc4fb9 02d77c2 f817cfc 12db0e1 2dc4fb9 02d77c2 e4a835d 990db9b e4a835d 2dc4fb9 12db0e1 c51f8c4 e28d45d 12db0e1 e28d45d c51f8c4 02d77c2 c51f8c4 02d77c2 c51f8c4 02d77c2 c51f8c4 e28d45d 4b20d59 e28d45d 3683c2c a971d1c 12db0e1 e28d45d c2c3825 8dc383f e28d45d 8dc383f a971d1c 8dc383f 4b20d59 c51f8c4 e28d45d a971d1c c51f8c4 e28d45d c51f8c4 02d77c2 c51f8c4 4b20d59 02d77c2 8dc383f a971d1c c2c3825 02d77c2 a971d1c 02d77c2 e28d45d a971d1c 8dc383f e4a835d e28d45d 02d77c2 e28d45d 8dc383f 4b20d59 a971d1c 8dc383f 12db0e1 e28d45d 12db0e1 7ac8cfa 12db0e1 02d77c2 12db0e1 c2c3825 12db0e1 02d77c2 12db0e1 02d77c2 12db0e1 02d77c2 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 02d77c2 12db0e1 427e302 12db0e1 e28d45d 02d77c2 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 3683c2c 12db0e1 e28d45d 12db0e1 e28d45d 12db0e1 e28d45d 2dc4fb9 e28d45d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
import gradio as gr
from faster_whisper import WhisperModel
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import requests
import base64
import tempfile
import os
import logging
import time
from datetime import datetime
from html.parser import HTMLParser
from fastapi import FastAPI, Request, Query
from fastapi.responses import JSONResponse
import uvicorn
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logger = logging.getLogger(__name__)
# Models
logger.info("Loading models...")
whisper_model = WhisperModel("tiny", device="cpu", compute_type="int8")
model_name = "HuggingFaceTB/SmolLM2-360M-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float32,
device_map="cpu",
low_cpu_mem_usage=True
)
logger.info("Models loaded!")
def search_parallel(query):
"""DuckDuckGo search"""
logger.info("[SEARCH] Starting...")
try:
response = requests.get(
'https://html.duckduckgo.com/html/',
params={'q': query},
headers={'User-Agent': 'Mozilla/5.0'},
timeout=1.5
)
if response.status_code == 200:
class DDGParser(HTMLParser):
def __init__(self):
super().__init__()
self.results = []
self.in_result = False
self.current_text = ""
def handle_starttag(self, tag, attrs):
if tag == 'a' and any(k == 'class' and 'result__a' in v for k, v in attrs):
self.in_result = True
def handle_data(self, data):
if self.in_result and data.strip():
self.current_text += data.strip() + " "
def handle_endtag(self, tag):
if tag == 'a' and self.in_result:
if self.current_text:
self.results.append(self.current_text.strip()[:120])
self.current_text = ""
self.in_result = False
parser = DDGParser()
parser.feed(response.text)
result = "\n".join([f"• {r}" for r in parser.results[:2]]) if parser.results else "No results"
logger.info("[SEARCH] ✓")
return result, "DuckDuckGo"
except:
pass
return "No search results", "None"
def generate_answer(text_input):
"""Generate answer"""
logger.info(f"[AI] Question: {text_input[:60]}...")
try:
if not text_input or not text_input.strip():
return "No input provided"
current_date = datetime.now().strftime("%B %d, %Y")
search_start = time.time()
search_results, search_engine = search_parallel(text_input)
logger.info(f"[AI] Search: {time.time()-search_start:.2f}s")
messages = [
{"role": "system", "content": f"Today is {current_date}. Answer briefly using search results (60-80 words)."},
{"role": "user", "content": f"Search:\n{search_results}\n\nQ: {text_input}\nA:"}
]
prompt = f"<|im_start|>system\n{messages[0]['content']}<|im_end|>\n<|im_start|>user\n{messages[1]['content']}<|im_end|>\n<|im_start|>assistant\n"
gen_start = time.time()
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=800)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=80,
temperature=0.7,
do_sample=True,
top_p=0.9,
top_k=40,
repetition_penalty=1.15,
pad_token_id=tokenizer.eos_token_id
)
answer = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True).strip()
logger.info(f"[AI] Gen: {time.time()-gen_start:.2f}s | ✓")
return f"{answer}\n\n**Source:** {search_engine}"
except Exception as e:
logger.error(f"[AI] Error: {str(e)}")
return f"Error: {str(e)}"
# FastAPI app
app = FastAPI()
@app.post("/api/ai")
async def api_ai_post(request: Request):
"""AI endpoint - POST with JSON body"""
try:
body = await request.body()
logger.info(f"[API AI POST] Raw body: {body}")
if not body:
return JSONResponse({"error": "Empty request body"}, status_code=400)
try:
data = await request.json()
except Exception as e:
logger.error(f"[API AI POST] JSON parse error: {str(e)}")
return JSONResponse({"error": f"Invalid JSON: {str(e)}"}, status_code=400)
logger.info(f"[API AI POST] Parsed data: {data}")
question = data.get("text", "")
if not question:
return JSONResponse({"error": "No 'text' field in JSON"}, status_code=400)
answer = generate_answer(question)
return JSONResponse({"answer": answer})
except Exception as e:
logger.error(f"[API AI POST] Error: {str(e)}")
return JSONResponse({"error": str(e)}, status_code=500)
@app.get("/api/ai")
async def api_ai_get(text: str = Query(..., description="Question text")):
"""AI endpoint - GET with query param (Pluely fallback)"""
try:
logger.info(f"[API AI GET] Question: {text}")
if not text:
return JSONResponse({"error": "No text parameter"}, status_code=400)
answer = generate_answer(text)
return JSONResponse({"answer": answer})
except Exception as e:
logger.error(f"[API AI GET] Error: {str(e)}")
return JSONResponse({"error": str(e)}, status_code=500)
@app.get("/health")
async def health():
return {"status": "ok", "model": "SmolLM2-360M"}
# Gradio UI
with gr.Blocks(title="Fast Q&A") as demo:
gr.Markdown("""
# ⚡ Ultra-Fast Q&A - SmolLM2-360M
## 🎯 Pluely Configuration
### Option 1: GET with Query Param (EASIEST - Windows Compatible)
```
curl https://archcoder-basic-app.hf.space/api/ai?text={{TEXT}}
```
**Response Path:** `answer`
### Option 2: POST with JSON (If Option 1 doesn't work)
```
curl -X POST https://archcoder-basic-app.hf.space/api/ai -H "Content-Type: application/json" --data-binary @- << EOF
{"text":"{{TEXT}}"}
EOF
```
**Response Path:** `answer`
""")
with gr.Tab("Test"):
test_input = gr.Textbox(label="Question")
test_btn = gr.Button("🚀 Test")
test_output = gr.Textbox(label="Answer", lines=8)
test_btn.click(fn=generate_answer, inputs=[test_input], outputs=[test_output])
app = gr.mount_gradio_app(app, demo, path="/")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)
|