Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,17 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# ===========================
|
| 7 |
-
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
| 8 |
-
ai = pipeline("text-generation", model=model_id, max_new_tokens=200)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
try:
|
| 15 |
-
output = ai(message)[0]["generated_text"]
|
| 16 |
return {"reply": output}
|
| 17 |
except:
|
| 18 |
-
return {"reply": "ERROR"}
|
| 19 |
-
|
| 20 |
-
# ===========================
|
| 21 |
-
# GRADIO APP (UI + API)
|
| 22 |
-
# ===========================
|
| 23 |
-
with gr.Blocks() as demo:
|
| 24 |
-
|
| 25 |
-
gr.Markdown("# 🚀 Space 1 — Ghost Model")
|
| 26 |
-
|
| 27 |
-
inp = gr.Textbox(label="Message")
|
| 28 |
-
out = gr.Textbox(label="Reply")
|
| 29 |
-
btn = gr.Button("Send")
|
| 30 |
-
btn.click(lambda x: chat_api(x)["reply"], inp, out)
|
| 31 |
-
|
| 32 |
-
# API endpoint
|
| 33 |
-
gr.Interface(fn=chat_api, inputs=gr.Textbox(), outputs=gr.JSON(), live=False, title="API Endpoint", description="Use /chat JSON POST")
|
| 34 |
-
|
| 35 |
-
# ===========================
|
| 36 |
-
# LAUNCH
|
| 37 |
-
# ===========================
|
| 38 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
ai = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", max_new_tokens=200)
|
| 6 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
class Msg(BaseModel):
|
| 9 |
+
message: str
|
| 10 |
+
|
| 11 |
+
@app.post("/chat")
|
| 12 |
+
def chat(msg: Msg):
|
| 13 |
try:
|
| 14 |
+
output = ai(msg.message)[0]["generated_text"]
|
| 15 |
return {"reply": output}
|
| 16 |
except:
|
| 17 |
+
return {"reply": "ERROR"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|