Vladislav Krasnov commited on
Commit
57e83fb
·
1 Parent(s): c1fbe89

Update space, changed language.

Browse files
Files changed (1) hide show
  1. app.py +38 -60
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
- if tokenizer.pad_token is None:
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 predict(message, history):
35
- """Обработчик сообщений"""
36
- try:
37
- # Формируем промпт
38
- prompt = ""
39
- if history:
40
- for human_msg, ai_msg in history:
41
- prompt += f"### Human: {human_msg}\n### Assistant: {ai_msg}\n"
42
- prompt += f"### Human: {message}\n### Assistant:"
43
-
44
- # Токенизация и генерация
45
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024)
46
-
47
- with torch.no_grad():
48
- outputs = model.generate(
49
- inputs.input_ids,
50
- max_new_tokens=256,
51
- temperature=0.7,
52
- do_sample=True,
53
- top_p=0.9,
54
- pad_token_id=tokenizer.pad_token_id,
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
- # Исправляем предупреждение: добавляем type='messages'
66
- demo = gr.ChatInterface(
67
- fn=predict,
 
 
 
 
 
 
68
  title="LiveCoder LLM API",
69
- description="Модель Phi-2 для помощи в написании кода",
70
- chatbot=gr.Chatbot(height=400, type='messages'), # ИСПРАВЛЕНО ЗДЕСЬ
71
- textbox=gr.Textbox(placeholder="Введите вопрос по программированию...", lines=3),
72
  examples=[
73
- ["Напиши hello world на C++"],
74
- ["Объясни принцип ООП"],
75
- ["Как работает указатель в C++?"]
76
  ]
77
  )
78
 
79
- if __name__ == "__main__":
80
- demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
 
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)