Vladislav Krasnov commited on
Commit ·
3c7b473
1
Parent(s): 57e83fb
Update space 7
Browse files
app.py
CHANGED
|
@@ -2,6 +2,13 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load model and tokenizer
|
| 6 |
model_name = "microsoft/phi-2"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
@@ -15,16 +22,17 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 15 |
)
|
| 16 |
|
| 17 |
def generate_response(message):
|
| 18 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 19 |
prompt = f"### Instruction: {message}\n### Response:"
|
| 20 |
|
| 21 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 22 |
-
attention_mask = inputs.get('attention_mask', None)
|
| 23 |
|
| 24 |
with torch.no_grad():
|
| 25 |
outputs = model.generate(
|
| 26 |
inputs.input_ids,
|
| 27 |
-
attention_mask=attention_mask,
|
| 28 |
max_new_tokens=256,
|
| 29 |
temperature=0.7,
|
| 30 |
do_sample=True,
|
|
@@ -36,23 +44,15 @@ def generate_response(message):
|
|
| 36 |
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 37 |
return response.strip()
|
| 38 |
|
| 39 |
-
# Create
|
| 40 |
interface = gr.Interface(
|
| 41 |
fn=generate_response,
|
| 42 |
-
inputs=gr.Textbox(
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
outputs=gr.Textbox(label="Model response", lines=10),
|
| 48 |
-
title="LiveCoder LLM API",
|
| 49 |
-
description="Phi-2 model for programming assistance",
|
| 50 |
-
examples=[
|
| 51 |
-
["Write a hello world program in C++"],
|
| 52 |
-
["Explain the OOP principle"],
|
| 53 |
-
["How does a pointer work in C++?"]
|
| 54 |
-
]
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# Launch
|
| 58 |
interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Hardcode your space details here
|
| 6 |
+
USERNAME = "sarekuwa" # Replace with your actual username
|
| 7 |
+
SPACE_NAME = "livecoder" # Replace with your actual space name
|
| 8 |
+
API_ENDPOINT = f"https://{USERNAME}-{SPACE_NAME}.hf.space/api/predict"
|
| 9 |
+
|
| 10 |
+
print(f"API Endpoint: {API_ENDPOINT}")
|
| 11 |
+
|
| 12 |
# Load model and tokenizer
|
| 13 |
model_name = "microsoft/phi-2"
|
| 14 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
def generate_response(message):
|
| 25 |
+
"""Process user input and generate response"""
|
| 26 |
+
if not message.strip():
|
| 27 |
+
return "Please enter a question."
|
| 28 |
+
|
| 29 |
prompt = f"### Instruction: {message}\n### Response:"
|
| 30 |
|
| 31 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
|
|
|
| 32 |
|
| 33 |
with torch.no_grad():
|
| 34 |
outputs = model.generate(
|
| 35 |
inputs.input_ids,
|
|
|
|
| 36 |
max_new_tokens=256,
|
| 37 |
temperature=0.7,
|
| 38 |
do_sample=True,
|
|
|
|
| 44 |
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 45 |
return response.strip()
|
| 46 |
|
| 47 |
+
# Create interface
|
| 48 |
interface = gr.Interface(
|
| 49 |
fn=generate_response,
|
| 50 |
+
inputs=gr.Textbox(label="Input", placeholder="Enter programming question...", lines=3),
|
| 51 |
+
outputs=gr.Textbox(label="Output", lines=10),
|
| 52 |
+
title="LiveCoder API",
|
| 53 |
+
description="LLM programming assistant",
|
| 54 |
+
allow_flagging="never"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Launch application
|
| 58 |
interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|