aifakepro commited on
Commit
897ace2
·
verified ·
1 Parent(s): 9c338f1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # Загрузка модели
6
+ model_id = "LiquidAI/LFM2-350M-Extract"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
8
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
9
+
10
+ system_prompt = """Identify and extract information matching the following schema.
11
+ Return data as a JSON object. Missing data should be omitted.
12
+ Schema:
13
+ - product: "Product name"
14
+ - price: "Price in dollars"
15
+ - quantity: "Number of items"
16
+ """
17
+
18
+ def extract_info(user_input):
19
+ messages = [
20
+ {"role": "system", "content": system_prompt},
21
+ {"role": "user", "content": user_input}
22
+ ]
23
+ inputs = tokenizer.apply_chat_template(messages, return_tensors="pt", return_dict=True).to(model.device)
24
+ outputs = model.generate(**inputs, max_new_tokens=256, temperature=0, do_sample=False)
25
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
26
+ return response
27
+
28
+ # Создаем интерфейс Gradio
29
+ demo = gr.Interface(
30
+ fn=extract_info,
31
+ inputs=gr.Textbox(label="Input Text"),
32
+ outputs=gr.Textbox(label="Extracted JSON")
33
+ )
34
+
35
+ demo.launch()