Spaces:
Build error
Build error
new
Browse files
app.py
CHANGED
|
@@ -1,48 +1,7 @@
|
|
| 1 |
-
import
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
MODEL_ID = "abdelac/tinyllama"
|
| 7 |
|
| 8 |
-
@
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
-
MODEL_ID,
|
| 13 |
-
torch_dtype=torch.float16,
|
| 14 |
-
device_map="auto"
|
| 15 |
-
)
|
| 16 |
-
return tokenizer, model
|
| 17 |
-
|
| 18 |
-
tokenizer, model = load_model()
|
| 19 |
-
|
| 20 |
-
def respond(message, history):
|
| 21 |
-
# Format chat history
|
| 22 |
-
prompt = ""
|
| 23 |
-
for user_msg, assistant_msg in history:
|
| 24 |
-
prompt += f"Human: {user_msg}\nAssistant: {assistant_msg}\n"
|
| 25 |
-
prompt += f"Human: {message}\nAssistant:"
|
| 26 |
-
|
| 27 |
-
# Tokenize
|
| 28 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 29 |
-
|
| 30 |
-
# Generate
|
| 31 |
-
outputs = model.generate(
|
| 32 |
-
**inputs,
|
| 33 |
-
max_new_tokens=256,
|
| 34 |
-
temperature=0.7,
|
| 35 |
-
do_sample=True,
|
| 36 |
-
pad_token_id=tokenizer.eos_token_id
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Decode
|
| 40 |
-
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 41 |
-
return response
|
| 42 |
-
|
| 43 |
-
# Create chat interface
|
| 44 |
-
gr.ChatInterface(
|
| 45 |
-
respond,
|
| 46 |
-
title="TinyLlama Chat",
|
| 47 |
-
description="Chat with TinyLlama model",
|
| 48 |
-
).launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
app = FastAPI()
|
|
|
|
| 4 |
|
| 5 |
+
@app.get("/")
|
| 6 |
+
def greet_json():
|
| 7 |
+
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|