matthoffner commited on
Commit
86f94f0
1 Parent(s): 0d521c3

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -0
main.py CHANGED
@@ -34,12 +34,45 @@ async def index():
34
  class ChatCompletionRequest(BaseModel):
35
  prompt: str
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  @app.post("/v1/chat/completions")
38
  async def chat(request: ChatCompletionRequest, response_mode=None):
39
  tokens = llm.tokenize(request.prompt)
40
  async def server_sent_events(chat_chunks, llm):
41
  for token in llm.generate(chat_chunks):
42
  yield llm.detokenize(token)
 
43
 
44
  return EventSourceResponse(server_sent_events(tokens, llm))
45
 
 
34
  class ChatCompletionRequest(BaseModel):
35
  prompt: str
36
 
37
+
38
+ @app.get("/demo")
39
+ async def demo():
40
+ html_content = """
41
+ <!DOCTYPE html>
42
+ <html>
43
+ <body>
44
+ <div id="content"></div>
45
+
46
+ <script>
47
+ var source = new EventSource("https://matthoffner-starchat-alpha.hf.space/stream");
48
+ source.onmessage = function(event) {
49
+ document.getElementById("content").innerHTML += event.data
50
+ };
51
+ </script>
52
+
53
+ </body>
54
+ </html>
55
+ """
56
+ return HTMLResponse(content=html_content, status_code=200)
57
+
58
+ @app.get("/stream")
59
+ async def chat(prompt = "Write a simple express erver"):
60
+ tokens = llm.tokenize(prompt)
61
+ async def server_sent_events(chat_chunks, llm):
62
+ yield prompt
63
+ for chat_chunk in llm.generate(chat_chunks):
64
+ yield llm.detokenize(chat_chunk)
65
+ yield ""
66
+
67
+ return EventSourceResponse(server_sent_events(tokens, llm))
68
+
69
  @app.post("/v1/chat/completions")
70
  async def chat(request: ChatCompletionRequest, response_mode=None):
71
  tokens = llm.tokenize(request.prompt)
72
  async def server_sent_events(chat_chunks, llm):
73
  for token in llm.generate(chat_chunks):
74
  yield llm.detokenize(token)
75
+ yield ""
76
 
77
  return EventSourceResponse(server_sent_events(tokens, llm))
78