Spaces:
Build error
Build error
Upload main.py
Browse files
main.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ctransformers import AutoModelForCausalLM
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
|
5 |
+
# Load Gemma-2B model
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("gemma-2b.Q4_K_S.gguf", max_new_tokens=8192, threads=3)
|
7 |
+
|
8 |
+
# Define Pydantic object for input validation
|
9 |
+
class Validation(BaseModel):
|
10 |
+
prompt: str
|
11 |
+
|
12 |
+
# Initialize FastAPI
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
+
# Define route for generating text
|
16 |
+
@app.post("/generate_text")
|
17 |
+
async def generate_text(item: Validation):
|
18 |
+
# Construct the input prompt
|
19 |
+
input_prompt = f"<|im_start|>system\n{{Below is an instruction that describes a task. Write a response that appropriately completes the request.}}<|im_end|>\n<|im_start|>user\n{{{item.prompt}}}<|im_end|>\n<|im_start|>assistant"
|
20 |
+
|
21 |
+
return llm(input_prompt)
|