File size: 2,998 Bytes
16cbd12
e01870d
16cbd12
 
e01870d
16cbd12
 
 
 
e01870d
16cbd12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e01870d
16cbd12
 
 
 
 
 
 
 
 
e01870d
16cbd12
 
 
 
 
 
 
 
e01870d
16cbd12
 
 
 
 
 
 
 
e01870d
16cbd12
 
 
 
 
 
 
e01870d
 
 
16cbd12
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
from transformers import T5Tokenizer, T5ForConditionalGeneration
import gradio as gr
import requests
import re

# تحميل نموذج CodeT5
model_name = "Salesforce/codet5-base"
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)

# وظيفة لجلب الأكواد من الإنترنت تلقائيًا (مثلاً GitHub Gist أو مستودعات GitHub)
def fetch_code_from_github(keyword, language="python"):
    try:
        # البحث عن الأكواد عبر GitHub API
        url = f"https://api.github.com/search/code?q={keyword}+language:{language}"
        headers = {"Accept": "application/vnd.github.v3+json"}
        response = requests.get(url, headers=headers)
        
        if response.status_code == 200:
            results = response.json()
            # استخراج الروابط والمحتوى
            items = results.get("items", [])
            if items:
                first_result = items[0]
                file_url = first_result["html_url"]
                return f"Code fetched from: {file_url}"
            else:
                return "No matching code found on GitHub."
        else:
            return f"Error fetching code: {response.status_code}"
    except Exception as e:
        return str(e)

# وظيفة للتعديل أو التوليد باستخدام CodeT5
def modify_or_generate_code(input_text, task="generate_code"):
    try:
        inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
        outputs = model.generate(**inputs, max_length=512, num_beams=4, early_stopping=True)
        generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
        return generated_code
    except Exception as e:
        return str(e)

# واجهة Gradio
def main_interface(task, input_text, fetch_keyword=None, language="python"):
    if task == "fetch_code":
        return fetch_code_from_github(fetch_keyword, language)
    elif task == "generate_code":
        return modify_or_generate_code(input_text, task)
    else:
        return "Invalid task selected."

# إنشاء واجهة Gradio
interface = gr.Interface(
    fn=main_interface,
    inputs=[
        gr.Radio(["fetch_code", "generate_code"], label="Task"),
        gr.Textbox(lines=5, placeholder="Enter your input text or description here...", label="Input Text"),
        gr.Textbox(placeholder="Enter keyword for fetching code...", label="Fetch Keyword (Optional)"),
        gr.Textbox(value="python", placeholder="Language (default: python)", label="Programming Language"),
    ],
    outputs="text",
    title="CodeT5 Code Assistant",
    description="Generate or fetch code snippets using CodeT5 with extended features.",
    examples=[
        ["generate_code", "Translate Python to Java: def add(a, b): return a + b", None, "python"],
        ["fetch_code", None, "factorial", "python"]
    ]
)

if __name__ == "__main__":
    interface.launch()