Spaces:
Sleeping
Sleeping
easyread.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!pip install -qqq datasets==3.5.0
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
from transformers import pipeline
|
6 |
+
from datasets import load_dataset
|
7 |
+
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
device
|
10 |
+
|
11 |
+
#from google.colab import userdata
|
12 |
+
#userdata.get('llama_easyread')
|
13 |
+
|
14 |
+
# map the image (description text) -> block of text
|
15 |
+
|
16 |
+
# <block>paragraph 1</block>
|
17 |
+
# <block>paragraph 2</block>
|
18 |
+
|
19 |
+
|
20 |
+
# description text of image -> (pass to embedding model) -> get vector embedding | => Compute cosine similarity -> we get similarity score 0-1 (1 means the same 0 means not the same)
|
21 |
+
# paragraph 1 -> (pass to embedding model) -> get vector embedding |
|
22 |
+
|
23 |
+
model_id = "meta-llama/Llama-3.2-1B-Instruct"
|
24 |
+
|
25 |
+
pipe = pipeline(
|
26 |
+
"text-generation",
|
27 |
+
model=model_id,
|
28 |
+
device=device,
|
29 |
+
torch_dtype=torch.bfloat16 if "cuda" in device else torch.float32,
|
30 |
+
)
|
31 |
+
|
32 |
+
messages = [
|
33 |
+
{"role":"system", "content": "You're a helpful EasyRead Assistant the simplifies complex documents or content. Follow the easy read guidelines. Only provide the simiplied content, for complex terms in the simplified text, always add a footnote for definitions."}
|
34 |
+
]
|
35 |
+
|
36 |
+
def add_and_generate(history, text):
|
37 |
+
messages.append({"role":"user","content": text})
|
38 |
+
prompt = pipe.tokenizer.apply_chat_template(
|
39 |
+
messages, tokenize=False, add_generation_prompt=True
|
40 |
+
)
|
41 |
+
# print(prompt)
|
42 |
+
out = pipe(prompt, max_new_tokens=150, do_sample=True, temperature=0.7, top_p=0.9)
|
43 |
+
reply = out[0]["generated_text"][len(prompt):]
|
44 |
+
messages.append({"role":"assistant","content":reply})
|
45 |
+
history.append((text, reply))
|
46 |
+
|
47 |
+
return history, ""
|
48 |
+
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
chatbot = gr.Chatbot()
|
51 |
+
txt = gr.Textbox(placeholder="Type here...")
|
52 |
+
txt.submit(add_and_generate, [chatbot, txt], [chatbot, txt])
|
53 |
+
|
54 |
+
demo.launch(debug=True)
|