Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,49 @@
|
|
| 1 |
-
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
class UserRequest(BaseModel):
|
| 7 |
-
prompt: str
|
| 8 |
-
|
| 9 |
-
app = FastAPI()
|
| 10 |
-
|
| 11 |
# Load the model and tokenizer
|
| 12 |
model_name = "Artples/L-MChat-7b"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
device = torch.device("cpu")
|
| 18 |
model.to(device)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load the model and tokenizer
|
| 6 |
model_name = "Artples/L-MChat-7b"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Ensure the model uses CPU
|
| 11 |
device = torch.device("cpu")
|
| 12 |
model.to(device)
|
| 13 |
|
| 14 |
+
def chat_with_model(json_input):
|
| 15 |
+
prompt = json_input['prompt']
|
| 16 |
+
# Tokenize the input prompt
|
| 17 |
+
inputs = tokenizer.encode(prompt, return_tensors="pt")
|
| 18 |
+
inputs = inputs.to(device)
|
| 19 |
+
|
| 20 |
+
# Generate a response
|
| 21 |
+
output = model.generate(inputs, max_length=100, num_return_sequences=1)
|
| 22 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
return {"choices": [{"text": response_text}]}
|
| 25 |
+
|
| 26 |
+
# Define the JSON input component
|
| 27 |
+
json_schema = {
|
| 28 |
+
"title": "Request",
|
| 29 |
+
"type": "object",
|
| 30 |
+
"properties": {
|
| 31 |
+
"prompt": {
|
| 32 |
+
"type": "string",
|
| 33 |
+
"description": "Enter your prompt here."
|
| 34 |
+
}
|
| 35 |
+
},
|
| 36 |
+
"required": ["prompt"]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=chat_with_model,
|
| 42 |
+
inputs=gr.inputs.JSON(schema=json_schema),
|
| 43 |
+
outputs="json",
|
| 44 |
+
title="Chat with L-MChat-7b",
|
| 45 |
+
description="API-like interface using Gradio to simulate OpenAI API behavior."
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Run the Gradio app
|
| 49 |
+
iface.launch()
|