Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,13 @@ import gradio as gr
|
|
2 |
import traceback
|
3 |
from transformers import pipeline
|
4 |
|
|
|
5 |
ai_analyzer = pipeline(
|
6 |
"text-generation",
|
7 |
model="Salesforce/codet5-base"
|
8 |
)
|
9 |
|
|
|
10 |
ERROR_SUGGESTIONS = {
|
11 |
"SyntaxError": {
|
12 |
"suggestion": "Kodun sözdiziminde hata var. Parantezleri veya iki nokta üst üste (:) işaretlerini kontrol edin.",
|
@@ -36,8 +38,25 @@ ERROR_SUGGESTIONS = {
|
|
36 |
"suggestion": "Sözlükte (dictionary) olmayan bir anahtara erişmeye çalışıyorsunuz.",
|
37 |
"example": "Doğru kullanım:\n```python\nmy_dict = {'a': 1, 'b': 2}\nprint(my_dict['a'])\n```"
|
38 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
}
|
40 |
|
|
|
41 |
def analyze_code(code):
|
42 |
try:
|
43 |
exec(code, {}) # Güvenli şekilde kodu çalıştır
|
@@ -46,6 +65,7 @@ def analyze_code(code):
|
|
46 |
except Exception as e:
|
47 |
error_type = type(e).__name__ # Hata tipini al
|
48 |
error_message = str(e) # Hata mesajı
|
|
|
49 |
suggestion = ERROR_SUGGESTIONS.get(error_type, {"suggestion": "Bu hata için özel bir çözümümüz yok.", "example": ""})
|
50 |
|
51 |
# Yapay zeka destekli analiz
|
@@ -53,7 +73,7 @@ def analyze_code(code):
|
|
53 |
ai_response = ai_analyzer(ai_prompt, max_length=150)[0]['generated_text']
|
54 |
|
55 |
# Çözüm önerisi ve doğru kullanım örneği
|
56 |
-
return f"❌ **{error_type} Hatası**:\n{error_message}\n\n💡 **Çözüm Önerisi:** {suggestion['suggestion']}\n\n📝 **Doğru Kullanım Örneği:**\n{suggestion['example']}\n\n🤖 **Yapay Zeka Açıklaması:** {ai_response}"
|
57 |
|
58 |
# ✅ Kullanıcı Dostu Gradio Arayüzü
|
59 |
with gr.Blocks() as demo:
|
|
|
2 |
import traceback
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Yapay zeka modelini yükle
|
6 |
ai_analyzer = pipeline(
|
7 |
"text-generation",
|
8 |
model="Salesforce/codet5-base"
|
9 |
)
|
10 |
|
11 |
+
# Hata türlerine özel çözüm önerileri
|
12 |
ERROR_SUGGESTIONS = {
|
13 |
"SyntaxError": {
|
14 |
"suggestion": "Kodun sözdiziminde hata var. Parantezleri veya iki nokta üst üste (:) işaretlerini kontrol edin.",
|
|
|
38 |
"suggestion": "Sözlükte (dictionary) olmayan bir anahtara erişmeye çalışıyorsunuz.",
|
39 |
"example": "Doğru kullanım:\n```python\nmy_dict = {'a': 1, 'b': 2}\nprint(my_dict['a'])\n```"
|
40 |
},
|
41 |
+
"AttributeError": {
|
42 |
+
"suggestion": "Bir nesnenin niteliklerine veya metodlarına yanlış şekilde erişilmeye çalışılmış.",
|
43 |
+
"example": "Doğru kullanım:\n```python\nmy_str = 'hello'\nprint(my_str.upper())\n```"
|
44 |
+
},
|
45 |
+
"FileNotFoundError": {
|
46 |
+
"suggestion": "Dosya bulunamadı. Dosya yolunu ve adını kontrol edin.",
|
47 |
+
"example": "Doğru kullanım:\n```python\nwith open('dosya.txt', 'r') as file:\n content = file.read()\n```"
|
48 |
+
},
|
49 |
+
"OverflowError": {
|
50 |
+
"suggestion": "Bir sayı çok büyük ya da küçük. Hesaplama sınırlarını aşmış olabilir.",
|
51 |
+
"example": "Doğru kullanım:\n```python\nimport math\nprint(math.exp(10))\n```"
|
52 |
+
},
|
53 |
+
"ValueError": {
|
54 |
+
"suggestion": "Veri tipi yanlış. Verilen veri beklenmeyen bir tipte olabilir.",
|
55 |
+
"example": "Doğru kullanım:\n```python\nnumber = int('10')\nprint(number)\n```"
|
56 |
+
},
|
57 |
}
|
58 |
|
59 |
+
# Kodun analiz edilmesi ve hataların tespiti
|
60 |
def analyze_code(code):
|
61 |
try:
|
62 |
exec(code, {}) # Güvenli şekilde kodu çalıştır
|
|
|
65 |
except Exception as e:
|
66 |
error_type = type(e).__name__ # Hata tipini al
|
67 |
error_message = str(e) # Hata mesajı
|
68 |
+
traceback_info = traceback.format_exc() # Hata ile ilgili detaylı izleme bilgisi
|
69 |
suggestion = ERROR_SUGGESTIONS.get(error_type, {"suggestion": "Bu hata için özel bir çözümümüz yok.", "example": ""})
|
70 |
|
71 |
# Yapay zeka destekli analiz
|
|
|
73 |
ai_response = ai_analyzer(ai_prompt, max_length=150)[0]['generated_text']
|
74 |
|
75 |
# Çözüm önerisi ve doğru kullanım örneği
|
76 |
+
return f"❌ **{error_type} Hatası**:\n{error_message}\n\n**Hata Satırı:**\n{traceback_info}\n\n💡 **Çözüm Önerisi:** {suggestion['suggestion']}\n\n📝 **Doğru Kullanım Örneği:**\n{suggestion['example']}\n\n🤖 **Yapay Zeka Açıklaması:** {ai_response}"
|
77 |
|
78 |
# ✅ Kullanıcı Dostu Gradio Arayüzü
|
79 |
with gr.Blocks() as demo:
|