AlexanderK commited on
Commit
18b049d
1 Parent(s): 7c04653

new app file

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import (
3
+ AutoModelForSeq2SeqLM,
4
+ AutoTokenizer,
5
+ AutoConfig,
6
+ pipeline,
7
+ )
8
+ import torch
9
+ from translator import translate_text # импортируем функцию переводчика
10
+
11
+
12
+ model_name = "sagard21/python-code-explainer"
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name, padding=True)
14
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
15
+ config = AutoConfig.from_pretrained(model_name)
16
+
17
+ if torch.cuda.is_available():
18
+ model = model.to('cuda') # запускаем модель на GPU, если доступно
19
+ model.eval()
20
+
21
+ pipe = pipeline("summarization", model=model_name, config=config, tokenizer=tokenizer)
22
+
23
+
24
+ def generate_text(text_prompt):
25
+ response = pipe(text_prompt)
26
+ english_explanation = response[0]['summary_text']
27
+ russian_explanation = translate_text(english_explanation) # переводим объяснение кода с англ на рус язык
28
+ return english_explanation, russian_explanation
29
+
30
+
31
+ textbox1 = gr.Textbox(value="""
32
+ class Solution(object):
33
+ def isValid(self, s):
34
+ stack = []
35
+ mapping = {")": "(", "}": "{", "]": "["}
36
+ for char in s:
37
+ if char in mapping:
38
+ top_element = stack.pop() if stack else '#'
39
+ if mapping[char] != top_element:
40
+ return False
41
+ else:
42
+ stack.append(char)
43
+ return not stack""")
44
+ textbox2 = gr.Textbox()
45
+ textbox3 = gr.Textbox()
46
+
47
+ if __name__ == "__main__":
48
+ with gr.Blocks() as demo:
49
+ gr.Interface(fn=generate_text, inputs=textbox1, outputs=[textbox2, textbox3])
50
+ demo.launch() # запускаем Gradio-интерфейс