Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +1,47 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
5 |
-
from pydantic import BaseModel
|
6 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
7 |
import torch
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
temperature=temperature,
|
39 |
-
do_sample=True,
|
40 |
-
pad_token_id=tokenizer.eos_token_id
|
41 |
-
)
|
42 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
43 |
-
return response
|
44 |
-
except Exception as e:
|
45 |
-
print(f"Error in generate_response: {str(e)}")
|
46 |
-
return f"Error generating response: {str(e)}"
|
47 |
-
|
48 |
-
class GenerationRequest(BaseModel):
|
49 |
-
prompt: str
|
50 |
-
max_length: int = 500
|
51 |
-
temperature: float = 0.7
|
52 |
-
|
53 |
-
class GenerationResponse(BaseModel):
|
54 |
-
response: str
|
55 |
-
|
56 |
-
@app.post("/generate", response_model=GenerationResponse)
|
57 |
-
async def generate_text(request: GenerationRequest):
|
58 |
-
try:
|
59 |
-
response = generate_response(
|
60 |
-
request.prompt,
|
61 |
-
max_length=request.max_length,
|
62 |
-
temperature=request.temperature
|
63 |
-
)
|
64 |
-
return GenerationResponse(response=response)
|
65 |
-
except Exception as e:
|
66 |
-
raise HTTPException(status_code=500, detail=str(e))
|
67 |
-
|
68 |
-
@app.get("/health")
|
69 |
-
async def health_check():
|
70 |
-
return {"status": "healthy"}
|
71 |
-
|
72 |
-
def gradio_generate(prompt, max_length, temperature):
|
73 |
-
return generate_response(prompt, int(max_length), float(temperature))
|
74 |
-
|
75 |
-
interface = gr.Interface(
|
76 |
-
fn=gradio_generate,
|
77 |
inputs=[
|
78 |
-
gr.Textbox(label="
|
79 |
-
gr.Slider(minimum=50, maximum=
|
80 |
-
gr.Slider(minimum=
|
81 |
],
|
82 |
-
outputs=gr.Textbox(label="
|
83 |
-
title="
|
84 |
-
description="Enter
|
85 |
examples=[
|
86 |
-
["
|
87 |
-
["Explain quantum computing in simple terms"],
|
88 |
-
["Create a recipe for chocolate chip cookies"]
|
89 |
]
|
90 |
)
|
91 |
|
92 |
-
|
93 |
-
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqGeneration
|
|
|
|
|
|
|
3 |
import torch
|
4 |
|
5 |
+
# Initialize model and tokenizer
|
6 |
+
model_name = "deepseek-ai/deepseek-model"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSeq2SeqGeneration.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()
|
|