-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
103 lines (88 loc) · 3.69 KB
/
Copy pathapp.py
File metadata and controls
103 lines (88 loc) · 3.69 KB
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import requests
import json
import gradio as gr
# Backend vLLM server endpoint
VLLM_URL = "http://127.0.0"
MODEL_NAME = "./Qwen2.5-7B-Summarizer"
def generate_summary(article_text, max_tokens, temperature):
if not article_text.strip():
return "Please input an article first!"
# Format prompt structure exactly like training data schema
payload = {
"model": MODEL_NAME,
"messages": [
{
"role": "system",
"content": "You are an expert assistant. Summarize the following article accurately, preserving technical terms, methods, and key focuses, in an easily readable manner that lay audiences can understand."
},
{
"role": "user",
"content": article_text.strip()
}
],
"max_tokens": int(max_tokens),
"temperature": float(temperature),
"stream": True # Enable text streaming for better UI experience
}
headers = {"Content-Type": "application/json"}
try:
response = requests.post(VLLM_URL, json=payload, headers=headers, stream=True)
if response.status_code != 200:
return f"Backend Error: {response.text}"
partial_summary = ""
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8').strip()
if decoded_line.startswith("data: "):
data_str = decoded_line[6:]
if data_str == "[DONE]":
break
try:
data_json = json.loads(data_str)
delta = data_json["choices"]["delta"]
if "content" in delta:
partial_summary += delta["content"]
yield partial_summary # Streams text word-by-word into the textbox
except Exception:
continue
except Exception as e:
yield f"Could not connect to vLLM server: {str(e)}\nMake sure your vLLM server is running on port 8000!"
# building Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("Qwen2.5-7B Custom Article Summarizer")
gr.Markdown("Fine-tuned using LoRA on the CNN/DailyMail dataset from hugging face, and optimized with vLLM acceleration.")
with gr.Row():
with gr.Column(scale=2):
input_box = gr.Textbox(
label="Source Article Text",
placeholder="Paste your long article or text here..",
lines=15
submit_btn = gr.Button("Generate Summary", variant="primary")
with gr.Column(scale=1):
output_box = gr.Textbox(
label="AI Summary Output",
lines=10,
interactive=False
)
with gr.Accordion("Advanced Generation Parameters", open=False):
max_tokens_slider = gr.Slider(
minimum=64, maximum=512, value=256, step=16,
label="Max Summary Length"
)
temp_slider = gr.Slider(
minimum=0.1, maximum=1.0, value=0.3, step=0.05,
label="Temperature (Creativity)"
)
# Start execution trigger
submit_btn.click(
fn=generate_summary,
inputs=[input_box, max_tokens_slider, temp_slider],
outputs=output_box
)
if __name__ == "__main__":
# Launch on localhost port 7860
demo.queue()
demo.launch(server_name="127.0.0.1", server_port=7860)
## If working on remote server via SSH, forward ports to local machine as follows:
## ssh -L 7860:127.0.0.1:7860 -L 8000:127.0.0.1:8000 user@your-server-ip