Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch, os
|
| 4 |
+
from fastapi import FastAPI, Request
|
| 5 |
+
import uvicorn
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto", torch_dtype=torch.float16)
|
| 10 |
+
|
| 11 |
+
PROMPT_TEMPLATE = """You are a GAIA final-answer extractor.
|
| 12 |
+
Extract only what follows "Final Answer:" from the text, or infer if missing.
|
| 13 |
+
Text:
|
| 14 |
+
{text}
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
def extract_final_answer(raw):
|
| 18 |
+
prompt = PROMPT_TEMPLATE.format(text=raw)
|
| 19 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 20 |
+
out = model.generate(**inputs, max_new_tokens=64, temperature=0.2)
|
| 21 |
+
return tokenizer.decode(out[0], skip_special_tokens=True).splitlines()[-1]
|
| 22 |
+
|
| 23 |
+
app = FastAPI()
|
| 24 |
+
|
| 25 |
+
@app.post("/api/predict")
|
| 26 |
+
async def predict(request: Request):
|
| 27 |
+
data = await request.json()
|
| 28 |
+
text = data.get("data", [""])[0]
|
| 29 |
+
return {"data": [extract_final_answer(text)]}
|
| 30 |
+
|
| 31 |
+
iface = gr.Interface(fn=extract_final_answer, inputs="text", outputs="text", title="Final Answer Agent")
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
import threading
|
| 35 |
+
threading.Thread(target=lambda: iface.launch(server_name="0.0.0.0", server_port=7863)).start()
|
| 36 |
+
uvicorn.run(app, host="0.0.0.0", port=7863)
|