manohar02 commited on
Commit
2d2d40c
·
verified ·
1 Parent(s): a81fab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -11
app.py CHANGED
@@ -1,16 +1,45 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- # Load your model
5
- summarizer = pipeline("summarization", model="manohar02/Llama-2-7b-finetune")
6
 
7
- # Define the summarization function
8
- def summarize(text):
9
- summary = summarizer(text, max_length=2048, min_length=10, do_sample=False)
10
- return summary[0]['summary_text']
 
 
 
 
 
 
 
 
 
11
 
12
- # Set up the Gradio interface
13
- iface = gr.Interface(fn=summarize, inputs="text", outputs="text", title="Text Summarizer")
14
 
15
- if __name__ == "__main__":
16
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()