Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,35 @@
|
|
1 |
-
|
2 |
-
from fastapi import FastAPI
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
|
6 |
app = FastAPI()
|
7 |
-
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
@app.get("/")
|
10 |
def home():
|
11 |
-
return{"message":"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
def gentxt(text : str):
|
15 |
-
output = pipe(text)
|
16 |
-
return({"output":output[0]["generated_text"]})
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize FastAPI app
|
5 |
app = FastAPI()
|
|
|
6 |
|
7 |
+
# Initialize text generation pipeline
|
8 |
+
def initialize_pipeline():
|
9 |
+
return pipeline("text2text-generation", model="google/flan-t5-small")
|
10 |
+
|
11 |
+
# Global variable to hold the pipeline instance
|
12 |
+
pipe = initialize_pipeline()
|
13 |
+
|
14 |
+
# Define home endpoint
|
15 |
@app.get("/")
|
16 |
def home():
|
17 |
+
return {"message": "Hello Siddhant"}
|
18 |
+
|
19 |
+
# Define generate endpoint with prompt parameter
|
20 |
+
@app.get("/generate")
|
21 |
+
def generate_text(
|
22 |
+
text: str = Query(None, description="Input text to generate from"),
|
23 |
+
prompt: str = Query(None, description="Optional prompt for fine-tuning the generated text"),
|
24 |
+
):
|
25 |
+
if not text and not prompt:
|
26 |
+
return {"error": "Please provide either 'text' or 'prompt' parameter."}
|
27 |
+
|
28 |
+
if prompt:
|
29 |
+
input_text = f"{text} {prompt}" if text else prompt
|
30 |
+
else:
|
31 |
+
input_text = text
|
32 |
+
|
33 |
+
output = pipe(input_text, max_length=100, do_sample=True, top_k=50)
|
34 |
|
35 |
+
return {"input_text": input_text, "output": output[0]["generated_text"]}
|
|
|
|
|
|