Spaces:
Sleeping
Sleeping
Commit
·
e9d2f90
1
Parent(s):
afa4e46
Initial commit
Browse files
app.py
CHANGED
|
@@ -47,11 +47,14 @@ import gc
|
|
| 47 |
gc.collect()
|
| 48 |
torch.cuda.empty_cache()
|
| 49 |
|
| 50 |
-
#
|
|
|
|
|
|
|
|
|
|
| 51 |
def init_db():
|
| 52 |
-
logger.debug("Initializing
|
| 53 |
try:
|
| 54 |
-
with sqlite3.connect(
|
| 55 |
cursor = conn.cursor()
|
| 56 |
cursor.execute('''CREATE TABLE IF NOT EXISTS CHRIS (
|
| 57 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -71,7 +74,7 @@ def init_db():
|
|
| 71 |
def save_to_memory(user_input, task_type, prompt, response):
|
| 72 |
logger.debug("Saving to database")
|
| 73 |
try:
|
| 74 |
-
with sqlite3.connect(
|
| 75 |
cursor = conn.cursor()
|
| 76 |
cursor.execute("INSERT INTO CHRIS (user_input, task_type, prompt, response, timestamp) VALUES (?, ?, ?, ?, ?)",
|
| 77 |
(user_input, task_type, prompt, response, datetime.now().isoformat()))
|
|
@@ -79,13 +82,14 @@ def save_to_memory(user_input, task_type, prompt, response):
|
|
| 79 |
logger.debug("Saved to DB")
|
| 80 |
except Exception as e:
|
| 81 |
logger.error(f"DB Save Error: {e}")
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
# دریافت تاریخچه
|
| 85 |
def get_history():
|
| 86 |
logger.debug("Retrieving history from database")
|
| 87 |
try:
|
| 88 |
-
with sqlite3.connect(
|
| 89 |
cursor = conn.cursor()
|
| 90 |
cursor.execute("SELECT id, user_input, task_type, prompt, response, timestamp FROM CHRIS ORDER BY timestamp DESC")
|
| 91 |
rows = cursor.fetchall()
|
|
@@ -109,12 +113,10 @@ def get_history():
|
|
| 109 |
def debug_models(text, language):
|
| 110 |
logger.debug(f"Debugging models with input: {text}, language: {language}")
|
| 111 |
try:
|
| 112 |
-
# تست مدل ترجمه
|
| 113 |
translated = translate_gen(text, max_length=300)[0]['translation_text']
|
| 114 |
logger.debug(f"Translated text: {translated}")
|
| 115 |
prompt = f"Write a complete, correct, and well-explained code in {language} to: {translated}"
|
| 116 |
logger.debug(f"Generated prompt: {prompt}")
|
| 117 |
-
# تست مدل کدنویسی
|
| 118 |
code_output = code_gen(prompt, max_length=512, num_beams=5)[0]['generated_text']
|
| 119 |
logger.debug(f"Generated code: {code_output}")
|
| 120 |
return f"Debug: Translation: {translated}\nPrompt: {prompt}\nCode: {code_output}"
|
|
@@ -159,6 +161,7 @@ def christopher(user_input, language, show_history):
|
|
| 159 |
return prompt, history
|
| 160 |
# تولید کد
|
| 161 |
response = generate_code(prompt)
|
|
|
|
| 162 |
save_to_memory(user_input, task_type, prompt, response)
|
| 163 |
history = get_history() if show_history else ""
|
| 164 |
return response, history
|
|
|
|
| 47 |
gc.collect()
|
| 48 |
torch.cuda.empty_cache()
|
| 49 |
|
| 50 |
+
# مسیر فایل دیتابیس
|
| 51 |
+
DB_PATH = "chris.db"
|
| 52 |
+
|
| 53 |
+
# ایجاد دیتابیس
|
| 54 |
def init_db():
|
| 55 |
+
logger.debug("Initializing database")
|
| 56 |
try:
|
| 57 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 58 |
cursor = conn.cursor()
|
| 59 |
cursor.execute('''CREATE TABLE IF NOT EXISTS CHRIS (
|
| 60 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 74 |
def save_to_memory(user_input, task_type, prompt, response):
|
| 75 |
logger.debug("Saving to database")
|
| 76 |
try:
|
| 77 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 78 |
cursor = conn.cursor()
|
| 79 |
cursor.execute("INSERT INTO CHRIS (user_input, task_type, prompt, response, timestamp) VALUES (?, ?, ?, ?, ?)",
|
| 80 |
(user_input, task_type, prompt, response, datetime.now().isoformat()))
|
|
|
|
| 82 |
logger.debug("Saved to DB")
|
| 83 |
except Exception as e:
|
| 84 |
logger.error(f"DB Save Error: {e}")
|
| 85 |
+
# نذار خطای دیتابیس جلوی ادامه کار رو بگیره
|
| 86 |
+
pass
|
| 87 |
|
| 88 |
# دریافت تاریخچه
|
| 89 |
def get_history():
|
| 90 |
logger.debug("Retrieving history from database")
|
| 91 |
try:
|
| 92 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 93 |
cursor = conn.cursor()
|
| 94 |
cursor.execute("SELECT id, user_input, task_type, prompt, response, timestamp FROM CHRIS ORDER BY timestamp DESC")
|
| 95 |
rows = cursor.fetchall()
|
|
|
|
| 113 |
def debug_models(text, language):
|
| 114 |
logger.debug(f"Debugging models with input: {text}, language: {language}")
|
| 115 |
try:
|
|
|
|
| 116 |
translated = translate_gen(text, max_length=300)[0]['translation_text']
|
| 117 |
logger.debug(f"Translated text: {translated}")
|
| 118 |
prompt = f"Write a complete, correct, and well-explained code in {language} to: {translated}"
|
| 119 |
logger.debug(f"Generated prompt: {prompt}")
|
|
|
|
| 120 |
code_output = code_gen(prompt, max_length=512, num_beams=5)[0]['generated_text']
|
| 121 |
logger.debug(f"Generated code: {code_output}")
|
| 122 |
return f"Debug: Translation: {translated}\nPrompt: {prompt}\nCode: {code_output}"
|
|
|
|
| 161 |
return prompt, history
|
| 162 |
# تولید کد
|
| 163 |
response = generate_code(prompt)
|
| 164 |
+
# ذخیره تو دیتابیس (اختیاری)
|
| 165 |
save_to_memory(user_input, task_type, prompt, response)
|
| 166 |
history = get_history() if show_history else ""
|
| 167 |
return response, history
|