1adaodoze commited on
Commit
40d1c74
·
verified ·
1 Parent(s): 2d28261

Rename App.py to app.py

Browse files
Files changed (1) hide show
  1. App.py → app.py +29 -24
App.py → app.py RENAMED
@@ -9,14 +9,14 @@ from llama_cpp import Llama
9
  import gradio as gr
10
  import uvicorn
11
 
12
- # --- Configurações do Modelo ---
13
  MODEL_REPO = "bartowski/Dolphin3.0-Llama3.1-8B-GGUF"
14
  MODEL_FILE = "Dolphin3.0-Llama3.1-8B-Q4_K_M.gguf"
15
  N_CTX = 4096
16
  N_THREADS = 4
17
 
18
- # --- Carregamento do Modelo ---
19
  llm = None
 
20
  def load_model():
21
  global llm
22
  if llm is None:
@@ -31,7 +31,7 @@ def load_model():
31
  print("Modelo carregado com sucesso.")
32
  return llm
33
 
34
- # --- Schemas da API OpenAI ---
35
  class ChatMessage(BaseModel):
36
  role: str
37
  content: str
@@ -43,10 +43,10 @@ class ChatCompletionRequest(BaseModel):
43
  max_tokens: Optional[int] = 512
44
  stream: bool = False
45
 
46
- # --- Cria o app FastAPI ---
47
- api_app = FastAPI()
48
 
49
- @api_app.get("/v1/models")
50
  async def list_models():
51
  return {
52
  "object": "list",
@@ -60,9 +60,10 @@ async def list_models():
60
  ],
61
  }
62
 
63
- @api_app.post("/v1/chat/completions")
64
  async def chat_completions(req: ChatCompletionRequest):
65
  model = load_model()
 
66
  prompt = ""
67
  for msg in req.messages:
68
  if msg.role == "system":
@@ -78,32 +79,36 @@ async def chat_completions(req: ChatCompletionRequest):
78
  output = model(prompt, max_tokens=req.max_tokens, temperature=req.temperature, stream=True)
79
  for chunk in output:
80
  token = chunk["choices"][0]["text"]
81
- data = {"id": "chatcmpl-local", "object": "chat.completion.chunk", "choices": [{"delta": {"content": token}, "index": 0, "finish_reason": None}]}
 
 
 
 
82
  yield f"data: {json.dumps(data)}\n\n"
83
  yield "data: [DONE]\n\n"
84
  return StreamingResponse(stream_generator(), media_type="text/event-stream")
85
- else:
86
- output = model(prompt, max_tokens=req.max_tokens, temperature=req.temperature, stream=False)
87
- text = output["choices"][0]["text"]
88
- return {
89
- "id": "chatcmpl-local",
90
- "object": "chat.completion",
91
- "created": 1700000000,
92
- "model": req.model,
93
- "choices": [{"index": 0, "message": {"role": "assistant", "content": text}, "finish_reason": "stop"}],
94
- "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
95
- }
96
 
97
- # --- Interface Gradio (obrigatória para o SDK) ---
 
 
 
 
 
 
 
 
 
 
 
98
  def dummy(x):
99
  return x
 
100
  demo = gr.Interface(fn=dummy, inputs="text", outputs="text")
101
 
102
- # --- Monta o Gradio DENTRO do FastAPI ---
103
- # A variável "app" é o que o Hugging Face procura.
104
- app = gr.mount_gradio_app(api_app, demo, path="/ui")
105
 
106
- # --- Apenas para rodar localmente (o HF ignora este bloco) ---
107
  if __name__ == "__main__":
108
  load_model()
109
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
9
  import gradio as gr
10
  import uvicorn
11
 
12
+ # --- Modelo ---
13
  MODEL_REPO = "bartowski/Dolphin3.0-Llama3.1-8B-GGUF"
14
  MODEL_FILE = "Dolphin3.0-Llama3.1-8B-Q4_K_M.gguf"
15
  N_CTX = 4096
16
  N_THREADS = 4
17
 
 
18
  llm = None
19
+
20
  def load_model():
21
  global llm
22
  if llm is None:
 
31
  print("Modelo carregado com sucesso.")
32
  return llm
33
 
34
+ # --- Schemas OpenAI ---
35
  class ChatMessage(BaseModel):
36
  role: str
37
  content: str
 
43
  max_tokens: Optional[int] = 512
44
  stream: bool = False
45
 
46
+ # --- FastAPI ---
47
+ api = FastAPI()
48
 
49
+ @api.get("/v1/models")
50
  async def list_models():
51
  return {
52
  "object": "list",
 
60
  ],
61
  }
62
 
63
+ @api.post("/v1/chat/completions")
64
  async def chat_completions(req: ChatCompletionRequest):
65
  model = load_model()
66
+
67
  prompt = ""
68
  for msg in req.messages:
69
  if msg.role == "system":
 
79
  output = model(prompt, max_tokens=req.max_tokens, temperature=req.temperature, stream=True)
80
  for chunk in output:
81
  token = chunk["choices"][0]["text"]
82
+ data = {
83
+ "id": "chatcmpl-local",
84
+ "object": "chat.completion.chunk",
85
+ "choices": [{"delta": {"content": token}, "index": 0, "finish_reason": None}],
86
+ }
87
  yield f"data: {json.dumps(data)}\n\n"
88
  yield "data: [DONE]\n\n"
89
  return StreamingResponse(stream_generator(), media_type="text/event-stream")
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ output = model(prompt, max_tokens=req.max_tokens, temperature=req.temperature, stream=False)
92
+ text = output["choices"][0]["text"]
93
+ return {
94
+ "id": "chatcmpl-local",
95
+ "object": "chat.completion",
96
+ "created": 1700000000,
97
+ "model": req.model,
98
+ "choices": [{"index": 0, "message": {"role": "assistant", "content": text}, "finish_reason": "stop"}],
99
+ "usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
100
+ }
101
+
102
+ # --- Gradio (obrigatório para o SDK) ---
103
  def dummy(x):
104
  return x
105
+
106
  demo = gr.Interface(fn=dummy, inputs="text", outputs="text")
107
 
108
+ # --- Monta o Gradio dentro do FastAPI ---
109
+ app = gr.mount_gradio_app(api, demo, path="/ui")
 
110
 
111
+ # --- Rodar localmente (o Hugging Face ignora este bloco) ---
112
  if __name__ == "__main__":
113
  load_model()
114
  uvicorn.run(app, host="0.0.0.0", port=7860)