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