Spaces:
Sleeping
Sleeping
Commit
·
7e44aab
1
Parent(s):
1453b63
Initial commit
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import sqlite3
|
|
| 4 |
from datetime import datetime
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
-
from transformers import pipeline, AutoTokenizer,
|
| 8 |
from huggingface_hub import login
|
| 9 |
|
| 10 |
# تنظیم logging
|
|
@@ -20,28 +20,17 @@ if not hf_token:
|
|
| 20 |
login(token=hf_token)
|
| 21 |
logger.debug("Hugging Face login successful")
|
| 22 |
|
| 23 |
-
# لود مدل
|
| 24 |
-
logger.debug("Loading
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
code_gen = pipeline("
|
| 29 |
-
logger.debug("
|
| 30 |
except Exception as e:
|
| 31 |
-
logger.error(f"Failed to load
|
| 32 |
raise
|
| 33 |
|
| 34 |
-
# لود مدل ترجمه
|
| 35 |
-
logger.debug("Loading Helsinki-NLP/opus-mt-fas-en model")
|
| 36 |
-
try:
|
| 37 |
-
translate_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-fas-en", cache_dir="/tmp/hf_cache")
|
| 38 |
-
translate_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-fas-en", cache_dir="/tmp/hf_cache")
|
| 39 |
-
translate_gen = pipeline("translation", model=translate_model, tokenizer=translate_tokenizer, max_length=300)
|
| 40 |
-
logger.debug("Helsinki-NLP/opus-mt-fas-en model loaded")
|
| 41 |
-
except Exception as e:
|
| 42 |
-
logger.error(f"Failed to load translation model: {e}")
|
| 43 |
-
translate_gen = None # Fallback: اگه مدل ترجمه لود نشد، از ورودی خام استفاده میکنیم
|
| 44 |
-
|
| 45 |
# آزادسازی حافظه
|
| 46 |
import gc
|
| 47 |
gc.collect()
|
|
@@ -108,71 +97,67 @@ def get_history():
|
|
| 108 |
logger.error(f"History retrieval error: {e}")
|
| 109 |
return f"خطا در دریافت تاریخچه: {e}"
|
| 110 |
|
| 111 |
-
# تابع دیباگ برای تست
|
| 112 |
-
def
|
| 113 |
-
logger.debug(f"Debugging
|
| 114 |
try:
|
| 115 |
-
|
| 116 |
-
translated = translate_gen(text, max_length=300)[0]['translation_text']
|
| 117 |
-
else:
|
| 118 |
-
translated = text # Fallback: اگه ترجمه کار نکرد، از ورودی خام استفاده کن
|
| 119 |
-
logger.debug(f"Translated text: {translated}")
|
| 120 |
-
prompt = f"Write a complete, correct, and well-explained code in {language} to: {translated}"
|
| 121 |
logger.debug(f"Generated prompt: {prompt}")
|
| 122 |
-
|
| 123 |
-
logger.debug(f"Generated
|
| 124 |
-
return f"Debug:
|
| 125 |
except Exception as e:
|
| 126 |
logger.error(f"Debug error: {e}")
|
| 127 |
return f"Debug error: {e}"
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
def
|
| 131 |
-
logger.debug(f"
|
| 132 |
try:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
logger.warning("Generated code is empty")
|
| 154 |
-
|
|
|
|
|
|
|
| 155 |
logger.debug(f"Generated code: {code_output}")
|
| 156 |
torch.cuda.empty_cache()
|
| 157 |
-
return code_output
|
| 158 |
except Exception as e:
|
| 159 |
logger.error(f"Code generation error: {e}")
|
| 160 |
-
return f"Code generation error: {e}"
|
| 161 |
|
| 162 |
# تابع اصلی
|
| 163 |
def christopher(user_input, language, show_history):
|
| 164 |
logger.debug(f"Processing input: {user_input}, language: {language}, show_history: {show_history}")
|
| 165 |
try:
|
| 166 |
task_type = "نوشتن کد کامل"
|
| 167 |
-
#
|
| 168 |
-
prompt =
|
| 169 |
-
if "error" in
|
| 170 |
history = get_history() if show_history else ""
|
| 171 |
-
return
|
| 172 |
-
# تولید کد
|
| 173 |
-
response = generate_code(prompt)
|
| 174 |
# ذخیره تو دیتابیس (اختیاری)
|
| 175 |
-
save_to_memory(user_input, task_type, prompt, response)
|
| 176 |
history = get_history() if show_history else ""
|
| 177 |
return response, history
|
| 178 |
except Exception as e:
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 8 |
from huggingface_hub import login
|
| 9 |
|
| 10 |
# تنظیم logging
|
|
|
|
| 20 |
login(token=hf_token)
|
| 21 |
logger.debug("Hugging Face login successful")
|
| 22 |
|
| 23 |
+
# لود مدل DeepSeek
|
| 24 |
+
logger.debug("Loading DeepSeek-Coder-6.7B-Instruct model")
|
| 25 |
try:
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained("DeepSeek/DeepSeek-Coder-6.7B-Instruct", cache_dir="/tmp/hf_cache")
|
| 27 |
+
model = AutoModelForCausalLM.from_pretrained("DeepSeek/DeepSeek-Coder-6.7B-Instruct", cache_dir="/tmp/hf_cache", torch_dtype=torch.float16, device_map="auto")
|
| 28 |
+
code_gen = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=1024, temperature=0.7, top_p=0.9)
|
| 29 |
+
logger.debug("DeepSeek-Coder-6.7B-Instruct model loaded")
|
| 30 |
except Exception as e:
|
| 31 |
+
logger.error(f"Failed to load DeepSeek model: {e}")
|
| 32 |
raise
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# آزادسازی حافظه
|
| 35 |
import gc
|
| 36 |
gc.collect()
|
|
|
|
| 97 |
logger.error(f"History retrieval error: {e}")
|
| 98 |
return f"خطا در دریافت تاریخچه: {e}"
|
| 99 |
|
| 100 |
+
# تابع دیباگ برای تست مدل
|
| 101 |
+
def debug_model(text, language):
|
| 102 |
+
logger.debug(f"Debugging model with input: {text}, language: {language}")
|
| 103 |
try:
|
| 104 |
+
prompt = f"""You are a coding assistant. The user provided this instruction in Persian: "{text}". Translate it to English, then write a complete, correct, and well-explained code in {language} based on the translated instruction. Return the English translation, the prompt, and the generated code."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
logger.debug(f"Generated prompt: {prompt}")
|
| 106 |
+
result = code_gen(prompt, max_new_tokens=1024, temperature=0.7, top_p=0.9, do_sample=True)[0]['generated_text']
|
| 107 |
+
logger.debug(f"Generated output: {result}")
|
| 108 |
+
return f"Debug: {result}"
|
| 109 |
except Exception as e:
|
| 110 |
logger.error(f"Debug error: {e}")
|
| 111 |
return f"Debug error: {e}"
|
| 112 |
|
| 113 |
+
# تولید پرامپت و کد
|
| 114 |
+
def generate_code_and_prompt(text, language):
|
| 115 |
+
logger.debug(f"Processing input: {text}, language: {language}")
|
| 116 |
try:
|
| 117 |
+
# پرامپت برای DeepSeek که هم ترجمه کنه و هم کد تولید کنه
|
| 118 |
+
prompt = f"""You are a coding assistant. The user provided this instruction in Persian: "{text}". First, translate the instruction to English. Then, write a complete, correct, and well-explained code in {language} based on the translated instruction. Return the English translation followed by the code."""
|
| 119 |
+
logger.debug(f"Generated prompt: {prompt}")
|
| 120 |
+
result = code_gen(prompt, max_new_tokens=1024, temperature=0.7, top_p=0.9, do_sample=True)[0]['generated_text']
|
| 121 |
+
|
| 122 |
+
# جدا کردن ترجمه و کد از خروجی
|
| 123 |
+
lines = result.split('\n')
|
| 124 |
+
translation = ""
|
| 125 |
+
code = []
|
| 126 |
+
is_code = False
|
| 127 |
+
for line in lines:
|
| 128 |
+
if line.startswith("Translation:"):
|
| 129 |
+
translation = line.replace("Translation:", "").strip()
|
| 130 |
+
elif line.startswith("```"):
|
| 131 |
+
is_code = not is_code
|
| 132 |
+
continue
|
| 133 |
+
elif is_code:
|
| 134 |
+
code.append(line)
|
| 135 |
+
|
| 136 |
+
code_output = "\n".join(code).strip()
|
| 137 |
+
if not code_output:
|
| 138 |
logger.warning("Generated code is empty")
|
| 139 |
+
code_output = "خطا: کد تولیدشده خالی است"
|
| 140 |
+
|
| 141 |
+
logger.debug(f"Translation: {translation}")
|
| 142 |
logger.debug(f"Generated code: {code_output}")
|
| 143 |
torch.cuda.empty_cache()
|
| 144 |
+
return translation, prompt, code_output
|
| 145 |
except Exception as e:
|
| 146 |
logger.error(f"Code generation error: {e}")
|
| 147 |
+
return "", f"Error prompt: {text}", f"Code generation error: {e}"
|
| 148 |
|
| 149 |
# تابع اصلی
|
| 150 |
def christopher(user_input, language, show_history):
|
| 151 |
logger.debug(f"Processing input: {user_input}, language: {language}, show_history: {show_history}")
|
| 152 |
try:
|
| 153 |
task_type = "نوشتن کد کامل"
|
| 154 |
+
# تولید ترجمه، پرامپت و کد
|
| 155 |
+
translation, prompt, response = generate_code_and_prompt(user_input, language)
|
| 156 |
+
if "error" in response.lower():
|
| 157 |
history = get_history() if show_history else ""
|
| 158 |
+
return response, history
|
|
|
|
|
|
|
| 159 |
# ذخیره تو دیتابیس (اختیاری)
|
| 160 |
+
save_to_memory(user_input, task_type, f"Translation: {translation}\nPrompt: {prompt}", response)
|
| 161 |
history = get_history() if show_history else ""
|
| 162 |
return response, history
|
| 163 |
except Exception as e:
|