Ashrafb commited on
Commit
cc29f21
1 Parent(s): 353f81e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +2 -74
main.py CHANGED
@@ -1,76 +1,4 @@
1
- from fastapi import FastAPI, Request, HTTPException
2
- from fastapi.responses import JSONResponse, FileResponse
3
- from fastapi.staticfiles import StaticFiles
4
- from huggingface_hub import InferenceClient
5
- import json
6
 
7
- app = FastAPI()
8
-
9
- client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
10
-
11
- SYSTEM_MESSAGE = (
12
- "You are a helpful, respectful and honest assistant. Always answer as helpfully "
13
- "as possible, while being safe. Your answers should not include any harmful, "
14
- "unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure "
15
- "that your responses are socially unbiased and positive in nature.\n\nIf a question "
16
- "does not make any sense, or is not factually coherent, explain why instead of "
17
- "answering something not correct. If you don't know the answer to a question, please "
18
- "don't share false information."
19
- "Always respond in the language of user prompt for each prompt ."
20
- )
21
- MAX_TOKENS = 2000
22
- TEMPERATURE = 0.7
23
- TOP_P = 0.95
24
-
25
- def respond(message, history: list[tuple[str, str]]):
26
- messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
27
-
28
- for val in history:
29
- if val[0]:
30
- messages.append({"role": "user", "content": val[0]})
31
- if val[1]:
32
- messages.append({"role": "assistant", "content": val[1]})
33
-
34
- messages.append({"role": "user", "content": message})
35
-
36
- response = client.chat_completion(
37
- messages,
38
- max_tokens=MAX_TOKENS,
39
- stream=True,
40
- temperature=TEMPERATURE,
41
- top_p=TOP_P,
42
- )
43
-
44
- for message in response: # Handle regular iteration
45
- yield message.choices[0].delta.content
46
-
47
- from fastapi.middleware.cors import CORSMiddleware
48
-
49
- app.add_middleware(
50
- CORSMiddleware,
51
- allow_origins=["https://artixiban-ll3.static.hf.space"], # Allow only this origin
52
- allow_credentials=True,
53
- allow_methods=["*"], # Allow all methods (GET, POST, etc.)
54
- allow_headers=["*"], # Allow all headers
55
- )
56
-
57
- @app.post("/generate/")
58
- async def generate(request: Request):
59
- allowed_origin = "https://artixiban-ll3.static.hf.space"
60
- origin = request.headers.get("origin")
61
- if origin != allowed_origin:
62
- raise HTTPException(status_code=403, detail="Origin not allowed")
63
- form = await request.form()
64
- prompt = form.get("prompt")
65
- history = json.loads(form.get("history", "[]")) # Default to empty history
66
-
67
- if not prompt:
68
- raise HTTPException(status_code=400, detail="Prompt is required")
69
-
70
- response_generator = respond(prompt, history)
71
- final_response = ""
72
- for part in response_generator:
73
- final_response += part
74
-
75
- return JSONResponse(content={"response": final_response})
76
 
 
 
1
+ import os
 
 
 
 
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ exec(os.environ.get('CODE'))