Spaces:
Sleeping
Sleeping
File size: 9,142 Bytes
b99026f 8f41d53 b99026f e57e114 8f41d53 b99026f e57e114 1c0599d e57e114 1c0599d e57e114 1c0599d e57e114 057b9bb b99026f 7d1ffa9 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f 091164c b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f 8f41d53 b99026f e57e114 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f e57e114 b99026f 8f41d53 b99026f e57e114 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
import os
import torch
from transformers import pipeline
import gradio as gr
import asyncio
import ipaddress
from typing import Tuple
from accelerate import Accelerator
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
accelerator = Accelerator()
gpt2_pipeline = accelerator.prepare(
pipeline("text-generation", model="Qwen/Qwen-1_8B-Chat", device=accelerator.device , trust_remote_code=True)
)
Najeb_pipeline = accelerator.prepare(
pipeline("text-generation", model="najeebjust/Najeeb", device=accelerator.device , trust_remote_code=True)
)
llama2_pipeline = accelerator.prepare(
pipeline("text-generation", model="Harikrishnan46624/finetuned_llama2-1.1b-chat", device=accelerator.device , trust_remote_code=True)
)
'''
gpt2_pipeline = pipeline("text-generation", model="Qwen/Qwen-1_8B-Chat", device=0 if torch.cuda.is_available() else -1, trust_remote_code=True)
Najeb_pipeline = pipeline("text-generation", model="najeebjust/Najeeb", device=0 if torch.cuda.is_available() else -1)
llama2_pipeline = pipeline("text-generation", model="Harikrishnan46624/finetuned_llama2-1.1b-chat", device=0 if torch.cuda.is_available() else -1)
'''
summarization_pipeline = pipeline("summarization", model="Falconsai/text_summarization", device=0 if torch.cuda.is_available() else -1)
previous_questions = []
async def generate_gpt2(question, max_length, num_beams, temperature):
return gpt2_pipeline(
question,
max_length=max_length,
num_return_sequences=1,
num_beams=num_beams,
do_sample=True,
top_k=30,
top_p=0.9,
temperature=temperature
)[0]['generated_text']
async def generate_Najeb(question, max_length, num_beams, temperature):
return Najeb_pipeline(
question,
max_length=max_length,
num_return_sequences=1,
num_beams=num_beams,
do_sample=True,
top_k=30,
top_p=0.85,
temperature=temperature
)[0]['generated_text']
async def generate_llama2(question, max_length, num_beams, temperature):
return llama2_pipeline(
question,
max_length=max_length,
num_return_sequences=1,
num_beams=num_beams,
do_sample=True,
top_k=30,
top_p=0.9,
temperature=temperature
)[0]['generated_text']
async def generate_responses_async(question, max_length=128, num_beams=2, temperature=0.5):
responses = {}
previous_questions.append(question)
gpt2_task = asyncio.create_task(generate_gpt2(question, max_length, num_beams, temperature))
Najeb_task = asyncio.create_task(generate_Najeb(question, max_length, num_beams, temperature))
llama2_task = asyncio.create_task(generate_llama2(question, max_length, num_beams, temperature))
gpt2_response, Najeb_response, llama2_response = await asyncio.gather(gpt2_task, Najeb_task, llama2_task)
responses['GPT-2'] = gpt2_response
responses['Najeb '] = Najeb_response
responses['LLaMA 2'] = llama2_response
combined_responses = f"GPT-2: {gpt2_response}\nNajeb: {Najeb_response}\nLLaMA 2: {llama2_response}"
summarized_response = summarization_pipeline(combined_responses, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
return {
"Najeb Answering Response": Najeb_response,
"GPT-2 Answering Response": gpt2_response,
"LLaMA 2 Answering Response": llama2_response,
"Summarized Answering Response": summarized_response,
"Previous Questions": "\n".join(previous_questions[-5:])
}
def get_network(ip_input: str) -> Tuple[ipaddress.IPv4Network, str]:
try:
if ip_input.count("/") == 0:
ip_input += "/24"
net = ipaddress.IPv4Network(ip_input, strict=False)
ip = ip_input.split("/")[0]
return (net, ip)
except ValueError:
return None, None
def calculate_subnet(ip_input: str) -> str:
network, ip = get_network(ip_input)
if network is None or ip is None:
return "Invalid IP Address or Subnet!"
network_address = network.network_address
broadcast_address = network.broadcast_address
usable_hosts = list(network.hosts())
num_usable_hosts = len(usable_hosts)
usable_hosts_range = f"{usable_hosts[0]} - {usable_hosts[-1]}" if usable_hosts else "NA"
octets = str(ip).split('.')
binary_octets = [bin(int(octet))[2:].zfill(8) for octet in octets]
bin_ip = '.'.join(binary_octets)
bin_addr = str(bin(int(network_address))[2:].zfill(32))
bin_addr = '.'.join([bin_addr[i:i+8] for i in range(0, len(bin_addr), 8)])
bin_mask = str(bin(int(network.netmask))[2:].zfill(32))
bin_mask = '.'.join([bin_mask[i:i+8] for i in range(0, len(bin_mask), 8)])
result = f"""
IP Address: {ip}
Address (bin): {bin_ip}
Network Address: {network_address}
Network Address (bin): {bin_addr}
Netmask: {network.netmask}
Netmask (bin): {bin_mask}
CIDR Notation: {network.prefixlen}
Broadcast Address: {broadcast_address}
Usable IP Range: {usable_hosts_range}
Number of Hosts: {network.num_addresses:,d}
Number of Usable Hosts: {num_usable_hosts:,d}
Wildcard Mask: {network.hostmask}
Private IP: {network.is_private}
"""
return result.strip()
def handle_mode_selection(mode, input_text, max_length, num_beams, temperature):
if mode == "AI Question Answering":
result = asyncio.run(generate_responses_async(input_text, max_length, num_beams, temperature))
return result, ""
else:
subnet_result = calculate_subnet(input_text)
return {"Subnet Calculation Result": subnet_result}, ""
custom_css = """
body {
background-color: #f0f8ff;
font-family: 'Arial', sans-serif;
color: #333;
}
h1 {
text-align: center;
color: #0066cc;
}
p {
text-align: center;
color: #333;
}
.gradio-container {
width: 80%;
margin: auto;
background-color: rgba(255, 255, 255, 0.8);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
border-radius: 10px;
}
.gr-button {
background-color: #0066cc;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.gr-button:hover {
background-color: #004c99;
}
.gr-textbox {
border: 2px solid #0066cc;
border-radius: 5px;
padding: 10px;
background-color: #fff;
color: #333;
}
.gr-slider {
color: #0066cc;
}
.gr-json {
background-color: rgba(240, 248, 255, 0.8);
border-radius: 10px;
padding: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
#image-container {
text-align: center;
position: relative;
}
#image-container img {
width: 1400px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#image-container button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 102, 204, 0.8);
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
#image-container button:hover {
background-color: rgba(0, 76, 153, 0.8);
}
"""
scroll_js = """
<script>
function scrollToTop() {
document.getElementById('target-section').scrollIntoView({behavior: 'smooth'});
}
</script>
"""
iface = gr.Blocks(css=custom_css)
with iface:
gr.Markdown(f"<h1>Welcome to Najeb</h1><p>AI Question & Subnet Calculator, Enter your question or IP address to generate answers or calculate subnets.</p>")
gr.HTML(f"""
<div id="image-container">
<img src="https://news.cornell.edu/sites/default/files/styles/story_thumbnail_xlarge/public/2024-07/robot-1280x720_0.jpg?itok=AF6MakCq" alt="AI Image">
<button onclick="scrollToTop()">Go to Top</button>
</div>
{scroll_js} <!-- Adding the JS to handle scrolling -->
""")
gr.Markdown("<div id='target-section'></div>")
with gr.Row():
mode_selector = gr.Radio(["AI Question Answering", "Subnet Calculation"], label="Select Mode", value="AI Question Answering")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="Enter your question or IP", placeholder="Type here...", lines=2)
max_length_slider = gr.Slider(minimum=50, maximum=1024, value=128, label="Max Length")
num_beams_slider = gr.Slider(minimum=1, maximum=10, value=2, label="Number of Beams", step=1)
temperature_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, label="Temperature", step=0.1)
submit_button = gr.Button("Submit")
with gr.Column():
output_box = gr.JSON(label="Response Output")
previous_questions_box = gr.Markdown("### Previous Questions\n")
submit_button.click(
handle_mode_selection,
inputs=[mode_selector, input_text, max_length_slider, num_beams_slider, temperature_slider],
outputs=[output_box, previous_questions_box]
)
iface.launch(share=True) |