aliMohammad16 commited on
Commit
f4df259
·
verified ·
1 Parent(s): bb9278e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -84
app.py CHANGED
@@ -1,93 +1,47 @@
1
- import os
2
  import gradio as gr
3
- from fastapi import FastAPI, HTTPException
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from pydantic import BaseModel
6
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
  import torch
8
 
9
- os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers_cache'
10
- os.makedirs('/tmp/transformers_cache', exist_ok=True)
11
-
12
- app = FastAPI(title="DeepSeek LLM Interface")
13
-
14
- app.add_middleware(
15
- CORSMiddleware,
16
- allow_origins=["*"],
17
- allow_credentials=True,
18
- allow_methods=["*"],
19
- allow_headers=["*"],
20
- )
21
-
22
- model_name = "deepseek-ai/deepseek-llm-7b-base"
23
- tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir='/tmp/transformers_cache')
24
- model = AutoModelForCausalLM.from_pretrained(
25
- model_name,
26
- cache_dir='/tmp/transformers_cache',
27
- torch_dtype=torch.float16,
28
- device_map="auto"
29
- )
30
-
31
- def generate_response(prompt, max_length=500, temperature=0.7):
32
- """Generate response using the DeepSeek model"""
33
- try:
34
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
35
- outputs = model.generate(
36
- **inputs,
37
- max_length=max_length,
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="Prompt", placeholder="Enter your prompt here..."),
79
- gr.Slider(minimum=50, maximum=1000, value=500, step=50, label="Max Length"),
80
- gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
81
  ],
82
- outputs=gr.Textbox(label="Generated Response"),
83
- title="DeepSeek LLM Interface",
84
- description="Enter a prompt to generate text using DeepSeek LLM",
85
  examples=[
86
- ["Write a short story about a mysterious garden"],
87
- ["Explain quantum computing in simple terms"],
88
- ["Create a recipe for chocolate chip cookies"]
89
  ]
90
  )
91
 
92
- app = gr.mount_gradio_app(app, interface, path="/")
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()