Spaces:
Build error
Build error
Austin Stockbridge
commited on
No more Inference Client bullshit
Browse files
app.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -15,34 +19,36 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
-
messages.append(
|
| 23 |
if val[1]:
|
| 24 |
-
messages.append(
|
| 25 |
|
| 26 |
-
messages.append(
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 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=[
|
|
@@ -59,6 +65,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load the model and tokenizer locally
|
| 6 |
+
model_name = "mergekit-community/Anti-Qwen2.5-Coder-0.5B-Instruct"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
|
| 9 |
|
| 10 |
+
# Ensure the model runs on GPU if available
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model = model.to(device)
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
|
|
|
| 19 |
temperature,
|
| 20 |
top_p,
|
| 21 |
):
|
| 22 |
+
# Prepare the conversation history for the model
|
| 23 |
+
messages = [f"System: {system_message}"]
|
| 24 |
|
| 25 |
for val in history:
|
| 26 |
if val[0]:
|
| 27 |
+
messages.append(f"User: {val[0]}")
|
| 28 |
if val[1]:
|
| 29 |
+
messages.append(f"Assistant: {val[1]}")
|
| 30 |
|
| 31 |
+
messages.append(f"User: {message}")
|
| 32 |
+
context = "\n".join(messages)
|
| 33 |
|
| 34 |
+
# Tokenize input
|
| 35 |
+
input_ids = tokenizer(context, return_tensors="pt", truncation=True, max_length=2048).input_ids.to(device)
|
| 36 |
|
| 37 |
+
# Generate response
|
| 38 |
+
output_ids = model.generate(
|
| 39 |
+
input_ids,
|
| 40 |
+
max_new_tokens=max_tokens,
|
| 41 |
temperature=temperature,
|
| 42 |
top_p=top_p,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
# Decode and yield response
|
| 48 |
+
response = tokenizer.decode(output_ids[0][input_ids.shape[-1]:], skip_special_tokens=True)
|
| 49 |
+
yield response
|
| 50 |
|
| 51 |
+
# Gradio interface setup
|
|
|
|
|
|
|
|
|
|
| 52 |
demo = gr.ChatInterface(
|
| 53 |
respond,
|
| 54 |
additional_inputs=[
|
|
|
|
| 65 |
],
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|