ngandugilbert commited on
Commit
1222634
·
verified ·
1 Parent(s): 38dfe22

Updated to API

Browse files
Files changed (1) hide show
  1. app.py +59 -50
app.py CHANGED
@@ -1,64 +1,73 @@
1
- import gradio as gr
 
 
2
  from huggingface_hub import InferenceClient
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
27
 
28
- response = ""
 
 
 
29
 
30
- for message in client.chat_completion(
 
 
31
  messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
  ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
63
  if __name__ == "__main__":
64
- demo.launch()
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from typing import List, Literal, Optional
4
  from huggingface_hub import InferenceClient
5
+ from fastapi.responses import JSONResponse
6
+ import uuid
7
+ import time
8
+ import uvicorn
9
 
10
+ app = FastAPI()
 
 
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
 
13
+ # OpenAI-compatible request message
14
+ class Message(BaseModel):
15
+ role: Literal["system", "user", "assistant"]
16
+ content: str
17
 
18
+ # OpenAI-compatible request body
19
+ class ChatCompletionRequest(BaseModel):
20
+ model: str = "zephyr-7b-beta"
21
+ messages: List[Message]
22
+ temperature: Optional[float] = 0.7
23
+ top_p: Optional[float] = 0.95
24
+ max_tokens: Optional[int] = 512
25
+ stream: Optional[bool] = False
 
26
 
27
+ # OpenAI-compatible response message
28
+ class Choice(BaseModel):
29
+ index: int
30
+ message: Message
31
+ finish_reason: Optional[str] = "stop"
32
 
33
+ # OpenAI-compatible full response
34
+ class ChatCompletionResponse(BaseModel):
35
+ id: str
36
+ object: str = "chat.completion"
37
+ created: int
38
+ model: str
39
+ choices: List[Choice]
40
 
41
+ @app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
42
+ async def chat_completions(request: ChatCompletionRequest):
43
+ # Build HuggingFace-style message list
44
+ messages = [{"role": m.role, "content": m.content} for m in request.messages]
45
 
46
+ # Generate chat completion
47
+ response_text = ""
48
+ for chunk in client.chat_completion(
49
  messages,
50
+ max_tokens=request.max_tokens,
51
+ temperature=request.temperature,
52
+ top_p=request.top_p,
53
+ stream=False,
54
  ):
55
+ response_text += chunk.choices[0].delta.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ # Build OpenAI-style response
58
+ chat_response = ChatCompletionResponse(
59
+ id=f"chatcmpl-{uuid.uuid4().hex}",
60
+ created=int(time.time()),
61
+ model=request.model,
62
+ choices=[
63
+ Choice(
64
+ index=0,
65
+ message=Message(role="assistant", content=response_text),
66
+ )
67
+ ]
68
+ )
69
+ return JSONResponse(content=chat_response.dict())
70
 
71
+ # Run this file directly
72
  if __name__ == "__main__":
73
+ uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)