Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +59 -51
- requirements.txt +7 -1
app.py
CHANGED
|
@@ -1,64 +1,72 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
+
import time
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2")
|
|
|
|
| 9 |
|
| 10 |
+
# Inference function
|
| 11 |
+
def chat_completion(messages, model_name="mock-gpt-model", max_tokens=512, temperature=0.1, stream=False):
|
| 12 |
+
if not messages:
|
| 13 |
+
return {
|
| 14 |
+
"error": "No messages provided."
|
| 15 |
+
}
|
| 16 |
|
| 17 |
+
# Rebuild prompt
|
| 18 |
+
prompt = ""
|
| 19 |
+
for msg in messages:
|
| 20 |
+
role = msg.get("role", "")
|
| 21 |
+
content = msg.get("content", "")
|
| 22 |
+
if role == "user":
|
| 23 |
+
prompt += f"User: {content}\n"
|
| 24 |
+
elif role == "assistant":
|
| 25 |
+
prompt += f"Assistant: {content}\n"
|
| 26 |
+
prompt += "Assistant:"
|
| 27 |
|
| 28 |
+
# Generate output
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=max_tokens,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
do_sample=True,
|
| 36 |
+
pad_token_id=tokenizer.eos_token_id
|
| 37 |
+
)
|
| 38 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
|
| 40 |
+
# Extract assistant reply
|
| 41 |
+
assistant_reply = generated_text[len(prompt):].strip()
|
| 42 |
|
| 43 |
+
return {
|
| 44 |
+
"id": "1337",
|
| 45 |
+
"object": "chat.completion",
|
| 46 |
+
"created": time.time(),
|
| 47 |
+
"model": model_name,
|
| 48 |
+
"choices": [{
|
| 49 |
+
"message": {
|
| 50 |
+
"role": "assistant",
|
| 51 |
+
"content": assistant_reply
|
| 52 |
+
}
|
| 53 |
+
}]
|
| 54 |
+
}
|
| 55 |
|
| 56 |
+
# Gradio API endpoint setup
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=chat_completion,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.JSON(label="messages"), # List[{"role":..., "content":...}]
|
| 61 |
+
gr.Textbox(label="model", value="mock-gpt-model"),
|
| 62 |
+
gr.Slider(minimum=1, maximum=1024, value=512, label="max_tokens"),
|
| 63 |
+
gr.Slider(minimum=0.0, maximum=1.0, value=0.1, label="temperature"),
|
| 64 |
+
gr.Checkbox(label="stream", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
],
|
| 66 |
+
outputs=gr.JSON(label="response"),
|
| 67 |
+
title="OpenAI-compatible Chat API (Gradio + Transformers)",
|
| 68 |
+
allow_flagging="never"
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,7 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
transformers
|
| 4 |
+
torch
|
| 5 |
+
pydantic
|
| 6 |
+
starlette
|
| 7 |
+
gradio
|