Spaces:
Runtime error
Runtime error
George Grigorev
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from arxiv2text import arxiv_to_text
|
3 |
+
|
4 |
+
import torch
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
6 |
+
|
7 |
+
def get_model(model_url="thepowerfuldeez/Qwen2-1.5B-Summarize"):
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-1.5B-Instruct")
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_url,
|
11 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
12 |
+
load_in_4bit=True,
|
13 |
+
attn_implementation="flash_attention_2",
|
14 |
+
)
|
15 |
+
return model, tokenizer
|
16 |
+
|
17 |
+
|
18 |
+
def call_llm(model, tokenizer, text):
|
19 |
+
messages = [
|
20 |
+
{"role": "system", "content": "You are helpful AI assistant."},
|
21 |
+
{"role": "user", "content": text},
|
22 |
+
]
|
23 |
+
input_ids = tokenizer.apply_chat_template(
|
24 |
+
messages, add_generation_prompt=True, return_tensors="pt"
|
25 |
+
)
|
26 |
+
new_tokens = model.generate(input_ids, max_new_tokens=512)[0][len(input_ids[0]) :]
|
27 |
+
output = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
28 |
+
return output
|
29 |
+
|
30 |
+
model, tokenizer = get_model()
|
31 |
+
|
32 |
+
def summarize_pdf(pdf_url):
|
33 |
+
extracted_text = arxiv_to_text(pdf_url)
|
34 |
+
summary = call_llm(model, tokenizer, f"Summarize following text: {extracted_text[:71000]}")
|
35 |
+
return summary
|
36 |
+
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=summarize_pdf,
|
39 |
+
inputs="text",
|
40 |
+
outputs="text",
|
41 |
+
title="Arxiv PDF Summarizer",
|
42 |
+
description="Enter the URL of an Arxiv PDF to get a summary."
|
43 |
+
)
|
44 |
+
|
45 |
+
interface.launch()
|