Spaces:
Sleeping
Sleeping
Darshan
commited on
Commit
•
023a520
1
Parent(s):
11b43df
use different app for testing
Browse files- Dockerfile +0 -3
- app.py +39 -71
- requirements.txt +6 -5
Dockerfile
CHANGED
@@ -6,12 +6,9 @@ COPY . .
|
|
6 |
|
7 |
# Set the working directory to /
|
8 |
WORKDIR /
|
9 |
-
VOLUME ["cache:/.cache"]
|
10 |
|
11 |
# Install requirements.txt
|
12 |
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
13 |
|
14 |
-
USER 1000
|
15 |
-
|
16 |
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
17 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
6 |
|
7 |
# Set the working directory to /
|
8 |
WORKDIR /
|
|
|
9 |
|
10 |
# Install requirements.txt
|
11 |
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
12 |
|
|
|
|
|
13 |
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -1,17 +1,35 @@
|
|
1 |
-
from
|
2 |
-
|
3 |
-
import
|
4 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
5 |
-
from IndicTransToolkit import IndicProcessor
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
|
8 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
os.environ["HF_HOME"] = "/.cache"
|
11 |
-
# Initialize FastAPI
|
12 |
-
app = FastAPI()
|
13 |
|
14 |
-
# Add CORS middleware
|
15 |
app.add_middleware(
|
16 |
CORSMiddleware,
|
17 |
allow_origins=["*"],
|
@@ -20,67 +38,17 @@ app.add_middleware(
|
|
20 |
allow_headers=["*"],
|
21 |
)
|
22 |
|
23 |
-
# Initialize models and processors
|
24 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(
|
25 |
-
"ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True
|
26 |
-
)
|
27 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
28 |
-
"ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True
|
29 |
-
)
|
30 |
-
ip = IndicProcessor(inference=True)
|
31 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
32 |
-
model = model.to(DEVICE)
|
33 |
-
|
34 |
-
|
35 |
-
def translate_text(sentences: List[str], target_lang: str):
|
36 |
-
try:
|
37 |
-
src_lang = "eng_Latn"
|
38 |
-
batch = ip.preprocess_batch(sentences, src_lang=src_lang, tgt_lang=target_lang)
|
39 |
-
inputs = tokenizer(
|
40 |
-
batch,
|
41 |
-
truncation=True,
|
42 |
-
padding="longest",
|
43 |
-
return_tensors="pt",
|
44 |
-
return_attention_mask=True,
|
45 |
-
).to(DEVICE)
|
46 |
-
|
47 |
-
with torch.no_grad():
|
48 |
-
generated_tokens = model.generate(
|
49 |
-
**inputs,
|
50 |
-
use_cache=True,
|
51 |
-
min_length=0,
|
52 |
-
max_length=256,
|
53 |
-
num_beams=5,
|
54 |
-
num_return_sequences=1,
|
55 |
-
)
|
56 |
-
|
57 |
-
with tokenizer.as_target_tokenizer():
|
58 |
-
generated_tokens = tokenizer.batch_decode(
|
59 |
-
generated_tokens.detach().cpu().tolist(),
|
60 |
-
skip_special_tokens=True,
|
61 |
-
clean_up_tokenization_spaces=True,
|
62 |
-
)
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
"source_language": src_lang,
|
68 |
-
"target_language": target_lang,
|
69 |
-
}
|
70 |
-
except Exception as e:
|
71 |
-
raise Exception(f"Translation failed: {str(e)}")
|
72 |
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
try:
|
83 |
-
result = translate_text(sentences=sentences, target_lang=target_lang)
|
84 |
-
return result
|
85 |
-
except Exception as e:
|
86 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
|
3 |
+
from .ConfigEnv import config
|
|
|
|
|
4 |
from fastapi.middleware.cors import CORSMiddleware
|
5 |
|
6 |
+
from langchain.llms import Clarifai
|
7 |
+
from langchain.chains import LLMChain
|
8 |
+
from langchain.prompts import PromptTemplate
|
9 |
+
from TextGen import app
|
10 |
+
|
11 |
+
|
12 |
+
class Generate(BaseModel):
|
13 |
+
text: str
|
14 |
+
|
15 |
+
|
16 |
+
def generate_text(prompt: str):
|
17 |
+
if prompt == "":
|
18 |
+
return {"detail": "Please provide a prompt."}
|
19 |
+
else:
|
20 |
+
prompt = PromptTemplate(template=prompt, input_variables=["Prompt"])
|
21 |
+
llm = Clarifai(
|
22 |
+
pat=config.CLARIFAI_PAT,
|
23 |
+
user_id=config.USER_ID,
|
24 |
+
app_id=config.APP_ID,
|
25 |
+
model_id=config.MODEL_ID,
|
26 |
+
model_version_id=config.MODEL_VERSION_ID,
|
27 |
+
)
|
28 |
+
llmchain = LLMChain(prompt=prompt, llm=llm)
|
29 |
+
llm_response = llmchain.run({"Prompt": prompt})
|
30 |
+
return Generate(text=llm_response)
|
31 |
|
|
|
|
|
|
|
32 |
|
|
|
33 |
app.add_middleware(
|
34 |
CORSMiddleware,
|
35 |
allow_origins=["*"],
|
|
|
38 |
allow_headers=["*"],
|
39 |
)
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
@app.get("/", tags=["Home"])
|
43 |
+
def api_home():
|
44 |
+
return {"detail": "Welcome to FastAPI TextGen Tutorial!"}
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
+
@app.post(
|
48 |
+
"/api/generate",
|
49 |
+
summary="Generate text from prompt",
|
50 |
+
tags=["Generate"],
|
51 |
+
response_model=Generate,
|
52 |
+
)
|
53 |
+
def inference(input_prompt: str):
|
54 |
+
return generate_text(prompt=input_prompt)
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
fastapi
|
2 |
uvicorn
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
1 |
+
fastapi==0.99.1
|
2 |
uvicorn
|
3 |
+
requests
|
4 |
+
pydantic==1.10.12
|
5 |
+
langchain
|
6 |
+
clarifai
|
7 |
+
Pillow
|