Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
|
7 |
-
# Define the
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
iface = gr.Interface(fn=summarize, inputs="text", outputs="text", title="Text Summarizer")
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain import HuggingFacePipeline, PromptTemplate, LLMChain
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
import transformers
|
5 |
+
import torch
|
6 |
|
7 |
+
# Define the Hugging Face model
|
8 |
+
model = "meta-llama/Llama-2-7b-chat-hf"
|
9 |
|
10 |
+
# Define the Hugging Face pipeline
|
11 |
+
pipeline = transformers.pipeline(
|
12 |
+
"text-generation", # task
|
13 |
+
model=model,
|
14 |
+
torch_dtype=torch.bfloat16,
|
15 |
+
trust_remote_code=True,
|
16 |
+
device_map="auto",
|
17 |
+
max_length=20000,
|
18 |
+
do_sample=True,
|
19 |
+
top_k=10,
|
20 |
+
num_return_sequences=1,
|
21 |
+
eos_token_id=AutoTokenizer.from_pretrained(model).eos_token_id
|
22 |
+
)
|
23 |
|
24 |
+
llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': 0})
|
|
|
25 |
|
26 |
+
# Define the template for summarization
|
27 |
+
template = """
|
28 |
+
Write a concise summary of the following text delimited by triple backquotes.
|
29 |
+
'''{text}'''
|
30 |
+
SUMMARY:
|
31 |
+
"""
|
32 |
+
|
33 |
+
prompt = PromptTemplate(template=template, input_variables=["text"])
|
34 |
+
|
35 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
36 |
+
|
37 |
+
# Function to generate summary
|
38 |
+
def generate_summary(text):
|
39 |
+
summary = llm_chain.run(text)
|
40 |
+
# Extract only the summary part from the output
|
41 |
+
return summary.strip()
|
42 |
+
|
43 |
+
# Create a Gradio interface
|
44 |
+
iface = gr.Interface(fn=generate_summary, inputs="text", outputs="text", title="LLaMA2 Summarizer")
|
45 |
+
iface.launch()
|