vbcalinao commited on
Commit
8bf6e06
·
1 Parent(s): 7381938

initial commit for Coding Assistant

Browse files
CodingAssistant/__init___.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+
3
+ app = FastAPI(title="Deploying my own CodingAssistant with FastAPI on Huggingface")
4
+
5
+ from CodingAssistant import router
CodingAssistant/env_config.py ADDED
File without changes
CodingAssistant/router.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+ from TextGen import app
11
+
12
+ class Generate(BaseModel):
13
+ text:str
14
+
15
+ def generate_text(prompt: str):
16
+ if prompt == "":
17
+ return {"detail": "Please provide a prompt."}
18
+ else:
19
+ prompt = PromptTemplate(template=prompt, input_variables=['Prompt'])
20
+
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
+
29
+ llmchain = LLMChain(
30
+ prompt=prompt,
31
+ llm=llm
32
+ )
33
+
34
+ llm_response = llmchain.run({"Prompt": prompt})
35
+ return Generate(text=llm_response)
36
+
37
+
38
+
39
+ app.add_middleware(
40
+ CORSMiddleware,
41
+ allow_origins=["*"],
42
+ allow_credentials=True,
43
+ allow_methods=["*"],
44
+ allow_headers=["*"],
45
+ )
46
+
47
+ @app.get("/", tags=["Home"])
48
+ def api_home():
49
+ return {'detail': 'Welcome!'}
50
+
51
+ @app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
52
+ def inference(input_prompt: str):
53
+ return generate_text(prompt=input_prompt)
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.10.9 image
2
+ FROM python:3.10.9
3
+
4
+ # Copy the current directory contents into the container at .
5
+ COPY . .
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 ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Optional
2
+
3
+ from fastapi import FastAPI
4
+
5
+ app = FastAPI()
6
+
7
+
8
+ @app.get("/")
9
+ async def root():
10
+ return {"message": "Hello World"}
11
+
12
+ @app.get("/items/{item_id}")
13
+ def read_item(item_id: int, q: Optional[str] = None):
14
+ return {"item_id": item_id, "q": q}
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi==0.99.1
2
+ uvicorn
3
+ requests
4
+ pydantic==1.10.12
5
+ langchain
6
+ clarifai
7
+ Pillow