Vladislav Krasnov
commited on
Commit
·
57e83fb
1
Parent(s):
c1fbe89
Update space, changed language.
Browse files
app.py
CHANGED
|
@@ -1,80 +1,58 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
SPACE_NAME = "livecoder" # Имя вашего Space
|
| 8 |
-
USERNAME = "sarekuwa" # Ваш username на Hugging Face
|
| 9 |
-
|
| 10 |
-
# Выводим endpoint сразу при запуске
|
| 11 |
-
print("=" * 50)
|
| 12 |
-
print(f"Space name: {SPACE_NAME}")
|
| 13 |
-
print(f"Username: {USERNAME}")
|
| 14 |
-
print(f"Local URL: http://0.0.0.0:7860")
|
| 15 |
-
print(f"Public API endpoint: https://{USERNAME}-{SPACE_NAME}.hf.space/run/predict")
|
| 16 |
-
print("=" * 50)
|
| 17 |
-
|
| 18 |
-
# Загрузка модели
|
| 19 |
model_name = "microsoft/phi-2"
|
| 20 |
-
print("Loading tokenizer...")
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 22 |
-
|
| 23 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 24 |
|
| 25 |
-
print("Loading model...")
|
| 26 |
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
model_name,
|
| 28 |
torch_dtype=torch.float32,
|
| 29 |
device_map="cpu",
|
| 30 |
trust_remote_code=True
|
| 31 |
)
|
| 32 |
-
print("Model loaded successfully!")
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
"""
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
eos_token_id=tokenizer.eos_token_id
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 59 |
-
response = response.split("### Human:")[0].strip()
|
| 60 |
-
return response
|
| 61 |
-
|
| 62 |
-
except Exception as e:
|
| 63 |
-
return f"Ошибка: {str(e)}"
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
fn=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
title="LiveCoder LLM API",
|
| 69 |
-
description="
|
| 70 |
-
chatbot=gr.Chatbot(height=400, type='messages'), # ИСПРАВЛЕНО ЗДЕСЬ
|
| 71 |
-
textbox=gr.Textbox(placeholder="Введите вопрос по программированию...", lines=3),
|
| 72 |
examples=[
|
| 73 |
-
["
|
| 74 |
-
["
|
| 75 |
-
["
|
| 76 |
]
|
| 77 |
)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 1 |
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)
|
| 8 |
+
tokenizer.pad_token = tokenizer.eos_token
|
|
|
|
| 9 |
|
|
|
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
model_name,
|
| 12 |
torch_dtype=torch.float32,
|
| 13 |
device_map="cpu",
|
| 14 |
trust_remote_code=True
|
| 15 |
)
|
|
|
|
| 16 |
|
| 17 |
+
def generate_response(message):
|
| 18 |
+
"""API function - takes text, returns response"""
|
| 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,
|
| 31 |
+
top_p=0.9,
|
| 32 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 33 |
+
eos_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
|
| 37 |
+
return response.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Create Gradio Interface
|
| 40 |
+
interface = gr.Interface(
|
| 41 |
+
fn=generate_response,
|
| 42 |
+
inputs=gr.Textbox(
|
| 43 |
+
label="Programming question",
|
| 44 |
+
placeholder="Example: Write a binary search function in C++...",
|
| 45 |
+
lines=3
|
| 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 the application
|
| 58 |
+
interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
|