Spaces:
Sleeping
Sleeping
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) | |