aliMohammad16 commited on
Commit
dc08424
·
verified ·
1 Parent(s): 95f59c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -41
app.py CHANGED
@@ -1,47 +1,22 @@
1
- import gradio as gr
 
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
5
- # Initialize model and tokenizer
6
- model_name = "deepseek-ai/deepseek-llm-1.3b"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
 
10
- def summarize_text(text, max_length=150, min_length=50):
11
- """
12
- Summarize the input text using the DeepSeek model
13
- """
14
- # Prepare the input
15
- inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
16
-
17
- # Generate summary
18
- summary_ids = model.generate(
19
- inputs["input_ids"],
20
- max_length=max_length,
21
- min_length=min_length,
22
- length_penalty=2.0,
23
- num_beams=4,
24
- early_stopping=True
25
- )
26
-
27
- # Decode and return the summary
28
- summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
29
- return summary
30
 
31
- # Create the Gradio interface
32
- iface = gr.Interface(
33
- fn=summarize_text,
34
- inputs=[
35
- gr.Textbox(label="Input Text", placeholder="Enter the text you want to summarize...", lines=10),
36
- gr.Slider(minimum=50, maximum=300, value=150, label="Maximum Summary Length"),
37
- gr.Slider(minimum=30, maximum=150, value=50, label="Minimum Summary Length")
38
- ],
39
- outputs=gr.Textbox(label="Summary"),
40
- title="Text Summarization with DeepSeek",
41
- description="Enter your text and get an AI-generated summary using the DeepSeek model.",
42
- examples=[
43
- ["The artificial intelligence revolution has transformed various sectors of the economy, from healthcare to finance. Machine learning algorithms are now capable of detecting diseases, predicting market trends, and automating complex tasks. This technological advancement has raised both excitement about the potential benefits and concerns about job displacement and ethical implications."],
44
- ]
45
- )
46
 
47
- iface.launch()
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  import torch
5
 
6
+ app = FastAPI()
 
 
 
7
 
8
+ # Load Model & Tokenizer
9
+ MODEL_NAME = "facebook/bart-large-cnn" # Small & fast summarization model
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME).to("cpu") # Use "cuda" if you have a GPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Define input format
14
+ class InputText(BaseModel):
15
+ text: str
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ @app.post("/summarize")
18
+ async def summarize_text(input_text: InputText):
19
+ inputs = tokenizer(input_text.text, return_tensors="pt", max_length=1024, truncation=True)
20
+ summary_ids = model.generate(inputs.input_ids, max_length=150, min_length=50, length_penalty=2.0)
21
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
22
+ return {"summary": summary}