Spaces:
Runtime error
Runtime error
Download app.py from tirthparmarx/gemma-api: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/tirthparmarx/gemma-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/tirthparmarx/gemma-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/tirthparmarx/gemma-api/resolve/main/app.py
1.28 kB
| from fastapi import FastAPI, Request | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
| import torch | |
| import uvicorn | |
| app = FastAPI(title="Phi-2 Free API") | |
| # ✅ Non-gated model (public, CPU-friendly) | |
| model_name = "microsoft/phi-2" | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32) | |
| pipe = pipeline( | |
| "text-generation", | |
| model=model, | |
| tokenizer=tokenizer, | |
| max_new_tokens=200, | |
| temperature=0.7, | |
| top_p=0.9, | |
| repetition_penalty=1.1, | |
| ) | |
| async def home(): | |
| return {"message": "✅ Phi-2 API is running!"} | |
| async def chat(request: Request): | |
| data = await request.json() | |
| prompt = data.get("prompt", "").strip() | |
| if not prompt: | |
| return {"error": "Please provide a 'prompt' in JSON."} | |
| # Generate text | |
| result = pipe(prompt, max_new_tokens=150, do_sample=True)[0]["generated_text"] | |
| # ✂️ Clean response: remove prompt echo | |
| cleaned = result.replace(prompt, "").strip() | |
| if not cleaned: | |
| cleaned = "(No response generated. Try a longer prompt or lower temperature.)" | |
| return {"response": cleaned} | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |