vakodiya commited on
Commit
202867c
1 Parent(s): ca2c26d

Create requirements.txt

Browse files

Update Dockerfile

Update main.py

Update Dockerfile

Update main.py

Update main.py

Update README.md

model added

Update Dockerfile

Update Dockerfile

Update requirements.txt

Update main.py

Update Dockerfile

Update main.py

Update main.py

Update main.py

Update main.py

Update main.py

Update main.py

Update main.py

Update main.py

Dockerfile CHANGED
@@ -1,14 +1,27 @@
 
1
  FROM python:3.9
2
 
 
3
  WORKDIR /code
4
 
 
5
  COPY ./requirements.txt /code/requirements.txt
6
 
 
7
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
 
9
- COPY ./__init__.py /code/__init__.py
10
- COPY ./credentials.env /code/credentials.env
11
- COPY ./rag_retriver.py /code/rag_retriver.py
12
- COPY ./main.py /code/main.py
 
 
 
 
 
 
 
 
 
13
 
14
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
1
+ # Use the official Python 3.9 image
2
  FROM python:3.9
3
 
4
+ # Set the working directory to /code
5
  WORKDIR /code
6
 
7
+ # Copy the current directory contents into the container at /code
8
  COPY ./requirements.txt /code/requirements.txt
9
 
10
+ # Install requirements.txt
11
  RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
 
13
+ # Set up a new user named "user" with user ID 1000
14
+ RUN useradd -m -u 1000 user
15
+ # Switch to the "user" user
16
+ USER user
17
+ # Set home to the user's home directory
18
+ ENV HOME=/home/user \
19
+ PATH=/home/user/.local/bin:$PATH
20
+
21
+ # Set the working directory to the user's home directory
22
+ WORKDIR $HOME/app
23
+
24
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
25
+ COPY --chown=user . $HOME/app
26
 
27
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
  title: UvicornGpt2
3
- emoji: 📉
4
- colorFrom: indigo
5
- colorTo: yellow
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
-
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: UvicornGpt2
3
+ emoji: 😻
4
+ colorFrom: purple
5
+ colorTo: pink
6
  sdk: docker
7
  pinned: false
8
+ license: mit
9
  ---
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
main.py CHANGED
@@ -1,36 +1,56 @@
 
 
1
  from fastapi import FastAPI
2
  from pydantic import BaseModel
3
- from transformers import GPT2Tokenizer, GPT2Model
4
  from langchain.prompts import PromptTemplate
5
 
6
  app = FastAPI()
7
 
8
  tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
9
- model = GPT2Model.from_pretrained('gpt2')
10
 
11
 
12
  class TextRequest(BaseModel):
13
- text: str
14
 
15
 
16
- def preprocess_text(text: str):
17
- return text.lower()
18
 
19
 
20
  def classify_text(question: str):
21
- prompt_template = PromptTemplate(template="Answer the following question and classify it: {question}", input_variables = ["question"], output_variables=["answer", "classification"])
22
- # Model loading
23
- format_prompt = prompt_template.format(question=question)
24
  encoded_input = tokenizer(format_prompt, return_tensors='pt')
25
- output = model(encoded_input)
26
- # chain = LLMChain(llm=llm, prompt=prompt_template, verbose=True)
27
- # response = chain({"question": question})
28
- return output
 
 
 
29
 
30
 
31
  @app.post("/classify")
32
  async def classify_text_endpoint(request: TextRequest):
33
- preprocessed_text = preprocess_text(request.text)
34
  response = classify_text(preprocessed_text)
35
- answer = response['text']
36
- return {"answer": answer}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from fastapi.staticfiles import StaticFiles
2
+ # from fastapi.responses import FileResponse
3
  from fastapi import FastAPI
4
  from pydantic import BaseModel
5
+ from transformers import GPT2Tokenizer, GPT2LMHeadModel
6
  from langchain.prompts import PromptTemplate
7
 
8
  app = FastAPI()
9
 
10
  tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
11
+ model = GPT2LMHeadModel.from_pretrained('gpt2')
12
 
13
 
14
  class TextRequest(BaseModel):
15
+ question: str
16
 
17
 
18
+ def preprocess_text(question: str):
19
+ return question.lower()
20
 
21
 
22
  def classify_text(question: str):
23
+ prompt_template = PromptTemplate(template="Answer the following question and classify it: {question}",
24
+ input_variables=["question"])
25
+ format_prompt = prompt_template.format(question=input_text)
26
  encoded_input = tokenizer(format_prompt, return_tensors='pt')
27
+ # Run the model
28
+ output = model.generate(**encoded_input) # Use generate method for text generation
29
+ # Decode the model output to text
30
+ decoded_output = tokenizer.decode(output[0])
31
+ response_text = decoded_output.split('\n\n')
32
+ answer=response_text[1]
33
+ return {"answer": answer}
34
 
35
 
36
  @app.post("/classify")
37
  async def classify_text_endpoint(request: TextRequest):
38
+ preprocessed_text = preprocess_text(request.question)
39
  response = classify_text(preprocessed_text)
40
+ return response
41
+
42
+
43
+ #
44
+ #
45
+ # @app.get("/infer_t5")
46
+ # def t5(input):
47
+ # preprocessed_text = preprocess_text(request.text)
48
+ # response = classify_text(preprocessed_text)
49
+ # output = pipe_flan(input)
50
+ # return {"output": output[0]["generated_text"]}
51
+ #
52
+ # app.mount("/", StaticFiles(directory="static", html=True), name="static")
53
+ #
54
+ # @app.get("/")
55
+ # def index() -> FileResponse:
56
+ # return FileResponse(path="/app/static/index.html", media_type="text/html")
models--gpt2/.no_exist/607a30d783dfa663caf39e06633721c8d4cfcd7e/added_tokens.json ADDED
File without changes
models--gpt2/.no_exist/607a30d783dfa663caf39e06633721c8d4cfcd7e/special_tokens_map.json ADDED
File without changes
models--gpt2/refs/main ADDED
@@ -0,0 +1 @@
 
 
1
+ 607a30d783dfa663caf39e06633721c8d4cfcd7e
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation_function": "gelu_new",
3
+ "architectures": [
4
+ "GPT2LMHeadModel"
5
+ ],
6
+ "attn_pdrop": 0.1,
7
+ "bos_token_id": 50256,
8
+ "embd_pdrop": 0.1,
9
+ "eos_token_id": 50256,
10
+ "initializer_range": 0.02,
11
+ "layer_norm_epsilon": 1e-05,
12
+ "model_type": "gpt2",
13
+ "n_ctx": 1024,
14
+ "n_embd": 768,
15
+ "n_head": 12,
16
+ "n_layer": 12,
17
+ "n_positions": 1024,
18
+ "resid_pdrop": 0.1,
19
+ "summary_activation": null,
20
+ "summary_first_dropout": 0.1,
21
+ "summary_proj_to_labels": true,
22
+ "summary_type": "cls_index",
23
+ "summary_use_proj": true,
24
+ "task_specific_params": {
25
+ "text-generation": {
26
+ "do_sample": true,
27
+ "max_length": 50
28
+ }
29
+ },
30
+ "vocab_size": 50257
31
+ }
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:248dfc3911869ec493c76e65bf2fcf7f615828b0254c12b473182f0f81d3a707
3
+ size 548105171
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"model_max_length": 1024}
models--gpt2/snapshots/607a30d783dfa663caf39e06633721c8d4cfcd7e/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.2.5
2
+ langchain-community==0.2.5
3
+ python-multipart==0.0.9
4
+ fastapi==0.111.0
5
+ pydantic==2.7.3
6
+ uvicorn==0.30.1
7
+ requests==2.32.3
8
+ transformers==4.41.2
9
+ torch==2.3.1