Spaces:
Runtime error
Runtime error
Yash Sachdeva
commited on
Commit
•
e5e2748
1
Parent(s):
4b73894
quuestion_paper
Browse files- question_paper.py +31 -44
question_paper.py
CHANGED
@@ -1,47 +1,34 @@
|
|
1 |
-
|
|
|
|
|
|
|
2 |
|
3 |
-
from
|
4 |
-
from
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
from langchain.chains import LLMChain
|
8 |
-
from langchain.prompts import PromptTemplate
|
9 |
-
from TextGen import app
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
llm_response = llmchain.run({"Prompt": prompt})
|
31 |
-
return Generate(text=llm_response)
|
32 |
-
|
33 |
-
app.add_middleware(
|
34 |
-
CORSMiddleware,
|
35 |
-
allow_origins=["*"],
|
36 |
-
allow_credentials=True,
|
37 |
-
allow_methods=["*"],
|
38 |
-
allow_headers=["*"],
|
39 |
-
)
|
40 |
-
|
41 |
-
@app.get("/", tags=["Home"])
|
42 |
-
def api_home():
|
43 |
-
return {'detail': 'Welcome to FastAPI TextGen Tutorial!'}
|
44 |
-
|
45 |
-
@app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
|
46 |
-
def inference(input_prompt: str):
|
47 |
-
return generate_text(prompt=input_prompt)
|
|
|
1 |
+
import time
|
2 |
+
import copy
|
3 |
+
import asyncio
|
4 |
+
import requests
|
5 |
|
6 |
+
from fastapi import FastAPI, Request
|
7 |
+
from llama_cpp import Llama
|
8 |
+
from sse_starlette import EventSourceResponse
|
9 |
+
# Load the model
|
10 |
+
print("Loading model...")
|
11 |
+
llm = Llama(model_path="./llama-2-13b-chat.ggmlv3.q4_1.bin") # change based on the location of models
|
12 |
+
print("Model loaded!")
|
13 |
|
14 |
+
app = FastAPI()
|
|
|
|
|
|
|
15 |
|
16 |
+
@app.get("/llama")
|
17 |
+
async def llama(request: Request, question:str):
|
18 |
+
stream = llm(
|
19 |
+
f"""{question}""",
|
20 |
+
max_tokens=100,
|
21 |
+
stop=["\n", " Q:"],
|
22 |
+
stream=True,
|
23 |
+
)
|
24 |
+
async def async_generator():
|
25 |
+
for item in stream:
|
26 |
+
yield item
|
27 |
+
async def server_sent_events():
|
28 |
+
async for item in async_generator():
|
29 |
+
if await request.is_disconnected():
|
30 |
+
break
|
31 |
+
result = copy.deepcopy(item)
|
32 |
+
text = result["choices"][0]["text"]
|
33 |
+
yield {"data": text}
|
34 |
+
return EventSourceResponse(server_sent_events())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|