Makhinur commited on
Commit
99c818b
·
verified ·
1 Parent(s): c250541

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +85 -0
main.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import List, Tuple
3
+ from fastapi import FastAPI, Form, HTTPException
4
+ from fastapi.middleware.cors import CORSMiddleware
5
+ from pydantic import BaseModel
6
+ from text_generation import Client
7
+
8
+ # Ensure the HF_TOKEN environment variable is set
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
+ if HF_TOKEN is None:
11
+ raise ValueError("Please set the HF_TOKEN environment variable.")
12
+
13
+ # Model and API setup
14
+ model_id = 'codellama/CodeLlama-34b-Instruct-hf'
15
+ API_URL = "https://api-inference.huggingface.co/models/" + model_id
16
+
17
+ client = Client(
18
+ API_URL,
19
+ headers={"Authorization": f"Bearer {HF_TOKEN}"},
20
+ )
21
+
22
+ EOS_STRING = "</s>"
23
+ EOT_STRING = "<EOT>"
24
+
25
+ app = FastAPI()
26
+
27
+ # Allow CORS for your frontend application
28
+ app.add_middleware(
29
+ CORSMiddleware,
30
+ allow_origins=["*"], # Change this to your frontend's URL in production
31
+ allow_credentials=True,
32
+ allow_methods=["*"],
33
+ allow_headers=["*"],
34
+ )
35
+
36
+ # Pydantic model for request body
37
+ class ChatRequest(BaseModel):
38
+ prompt: str
39
+ history: List[Tuple[str, str]]
40
+
41
+ DEFAULT_SYSTEM_PROMPT = """\
42
+ You are a helpful, respectful and honest assistant with a deep knowledge of code and software design. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.\
43
+ """
44
+
45
+ def get_prompt(message: str, chat_history: List[Tuple[str, str]],
46
+ system_prompt: str) -> str:
47
+ texts = [f'<s>[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n']
48
+ do_strip = False
49
+ for user_input, response in chat_history:
50
+ user_input = user_input.strip() if do_strip else user_input
51
+ do_strip = True
52
+ texts.append(f'{user_input} [/INST] {response.strip()} </s><s>[INST] ')
53
+ message = message.strip() if do_strip else message
54
+ texts.append(f'{message} [/INST]')
55
+ return ''.join(texts)
56
+
57
+ @app.post("/generate/")
58
+ async def generate_response(prompt: str = Form(...), history: str = Form(...)):
59
+ try:
60
+ chat_history = eval(history) # Convert history string back to list
61
+ system_prompt = DEFAULT_SYSTEM_PROMPT
62
+ message = prompt
63
+
64
+ prompt_text = get_prompt(message, chat_history, system_prompt)
65
+
66
+ generate_kwargs = dict(
67
+ max_new_tokens=1024,
68
+ do_sample=True,
69
+ top_p=0.9,
70
+ top_k=50,
71
+ temperature=0.1,
72
+ )
73
+
74
+ stream = client.generate_stream(prompt_text, **generate_kwargs)
75
+ output = ""
76
+ for response in stream:
77
+ if any([end_token in response.token.text for end_token in [EOS_STRING, EOT_STRING]]):
78
+ break
79
+ else:
80
+ output += response.token.text
81
+
82
+ return {"response": output}
83
+
84
+ except Exception as e:
85
+ raise HTTPException(status_code=500, detail=str(e))