Vladislav Krasnov commited on
Commit ·
fd483eb
1
Parent(s): 3c7b473
Update space 8
Browse files- app.py +21 -25
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -2,13 +2,6 @@ import gradio as gr
|
|
| 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)
|
|
@@ -26,27 +19,30 @@ def generate_response(message):
|
|
| 26 |
if not message.strip():
|
| 27 |
return "Please enter a question."
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 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",
|
|
|
|
| 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)
|
|
|
|
| 19 |
if not message.strip():
|
| 20 |
return "Please enter a question."
|
| 21 |
|
| 22 |
+
try:
|
| 23 |
+
prompt = f"### Instruction: {message}\n### Response:"
|
| 24 |
+
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 26 |
+
|
| 27 |
+
with torch.no_grad():
|
| 28 |
+
outputs = model.generate(
|
| 29 |
+
inputs.input_ids,
|
| 30 |
+
max_new_tokens=256,
|
| 31 |
+
temperature=0.7,
|
| 32 |
+
do_sample=True,
|
| 33 |
+
top_p=0.9,
|
| 34 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 35 |
+
eos_token_id=tokenizer.eos_token_id
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 39 |
+
return response.strip()
|
| 40 |
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error generating response: {str(e)}"
|
| 43 |
|
|
|
|
| 44 |
interface = gr.Interface(
|
| 45 |
+
fn=generate_response, # Connect function to interface
|
| 46 |
inputs=gr.Textbox(label="Input", placeholder="Enter programming question...", lines=3),
|
| 47 |
outputs=gr.Textbox(label="Output", lines=10),
|
| 48 |
title="LiveCoder API",
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
torch
|
| 2 |
-
transformers
|
|
|
|
| 3 |
accelerate
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers>=4.35.0
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
accelerate
|