Spaces:
Sleeping
Sleeping
File size: 8,332 Bytes
b99026f 8f41d53 b99026f 8f41d53 057b9bb b99026f 091164c 7c0653d 057b9bb c7fda9c 057b9bb b99026f 091164c b99026f 091164c b99026f c7fda9c 091164c b99026f 091164c b99026f c7fda9c b99026f 091164c b99026f 057b9bb b99026f c7fda9c b99026f 057b9bb c7fda9c b99026f 057b9bb c7fda9c b99026f c7fda9c b99026f 091164c b99026f c7fda9c b99026f 091164c b99026f 091164c b99026f 8f41d53 b99026f 091164c b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 8f41d53 b99026f 057b9bb b99026f 057b9bb b99026f 057b9bb b99026f 8f41d53 b99026f 057b9bb c7fda9c 057b9bb |
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 |
import os
import torch
from transformers import pipeline
import gradio as gr
import asyncio
import ipaddress
from typing import Tuple
# تعيين المتغيرات البيئية لتهيئة PyTorch لاستخدام الـ GPU إذا كان متاحًا
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
# الحصول على التوكن من البيئة
token = os.getenv("HF_TOKEN")
# إعداد الأنابيب للموديلات المختلفة باستخدام PyTorch
device = 0 if torch.cuda.is_available() else -1
Najeb_pipeline = pipeline("text-generation", model="sohiebwedyan/NAJEB_BOT", token=token, device=device)
gpt2_pipeline = pipeline("text-generation", model="Qwen/Qwen-1_8B-Chat", device=device, trust_remote_code=True)
#llama2_pipeline = pipeline("text-generation", model="Harikrishnan46624/finetuned_llama2-1.1b-chat", device=device)
summarization_pipeline = pipeline("summarization", model="Falconsai/text_summarization", device=device)
previous_questions = []
# توليد الردود باستخدام GPT-2
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=50,
top_p=0.95,
temperature=temperature
)[0]['generated_text']
# توليد الردود باستخدام Najeb
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']
'''
# توليد الردود باستخدام LLaMA 2
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=50,
top_p=0.95,
temperature=temperature
)[0]['generated_text']'''
# التعامل مع الردود بشكل غير متزامن
async def generate_responses_async(question, max_length=128, num_beams=2, temperature=0.5):
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 = await asyncio.gather(gpt2_task, Najeb_task, llama2_task)
# دمج الردود و تلخيصها
combined_responses = f"GPT-2: {gpt2_response}\nNajeb: {Najeb_response}"
summarized_response = summarization_pipeline(combined_responses, max_length=150, min_length=50, do_sample=False)[0]['summary_text']
return {
"GPT-2 Answer": gpt2_response,
"Najeb Answer": Najeb_response,
#"LLaMA 2 Answer": llama2_response,
"Summarized Answer": summarized_response,
"Previous Questions": "\n".join(previous_questions[-5:])
}
# تحديد طريقة الحساب بناءً على المدخل
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 (
f"**GPT-2 Model Response:**\n{result['GPT-2 Answer']}",
f"**Najeb Model Response:**\n{result['Najeb Answer']}",
#f"**LLaMA 2 Model Response:**\n{result['LLaMA 2 Answer']}",
f"**Summarized Response:**\n{result['Summarized Answer']}",
f"**Previous Questions:**\n{result['Previous Questions']}"
)
else:
subnet_result = calculate_subnet(input_text)
return subnet_result, "", "", "", ""
# الحصول على الشبكة وعنوان الـ IP
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()
# تحديد التصميم المخصص
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: 100%;
max-width: 500px;
margin-bottom: 10px;
}
#image-container button {
position: absolute;
top: 10px;
left: 10px;
background-color: rgba(0, 0, 0, 0.5);
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
}
"""
# إعداد واجهة Gradio
gr.Interface(
fn=handle_mode_selection,
inputs=[
gr.Dropdown(choices=["AI Question Answering", "Subnet Calculation"], label="Select Mode"),
gr.Textbox(label="Input", placeholder="Ask your question or enter an IP address/subnet..."),
gr.Slider(minimum=50, maximum=1024, step=1, value=128, label="Max Length"),
gr.Slider(minimum=1, maximum=10, step=1, value=2, label="Num Beams"),
gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.5, label="Temperature")
],
outputs=[
gr.Markdown(label="GPT-2 Answer"),
gr.Markdown(label="Najeb Answer"),
#gr.Markdown(label="LLaMA 2 Answer"),
gr.Markdown(label="Summarized Answer"),
gr.Markdown(label="Previous Questions")
],
css=custom_css,
live=True
).launch(debug=True)
|