Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ from huggingface_hub import InferenceClient
|
|
| 8 |
# ==============================
|
| 9 |
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
-
|
| 12 |
if not HF_TOKEN:
|
| 13 |
raise EnvironmentError("HF_TOKEN is not set in environment variables.")
|
| 14 |
|
|
@@ -50,12 +49,16 @@ def save_code_to_file(code: str) -> str:
|
|
| 50 |
return file_path
|
| 51 |
|
| 52 |
# ==============================
|
| 53 |
-
# Chat Logic
|
| 54 |
# ==============================
|
| 55 |
|
| 56 |
def chat_stream(user_message, history):
|
| 57 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
messages.append({"role": "user", "content": user_message})
|
| 60 |
|
| 61 |
assistant_text = ""
|
|
@@ -69,31 +72,15 @@ def chat_stream(user_message, history):
|
|
| 69 |
for chunk in stream:
|
| 70 |
delta = chunk.choices[0].delta.content or ""
|
| 71 |
assistant_text += delta
|
| 72 |
-
yield (
|
| 73 |
-
history + [
|
| 74 |
-
{"role": "user", "content": user_message},
|
| 75 |
-
{"role": "assistant", "content": assistant_text}
|
| 76 |
-
],
|
| 77 |
-
"",
|
| 78 |
-
None,
|
| 79 |
-
""
|
| 80 |
-
)
|
| 81 |
|
| 82 |
extracted_code = extract_code(assistant_text)
|
| 83 |
file_path = save_code_to_file(extracted_code) if extracted_code else None
|
| 84 |
|
| 85 |
-
yield (
|
| 86 |
-
history + [
|
| 87 |
-
{"role": "user", "content": user_message},
|
| 88 |
-
{"role": "assistant", "content": assistant_text}
|
| 89 |
-
],
|
| 90 |
-
extracted_code,
|
| 91 |
-
file_path,
|
| 92 |
-
""
|
| 93 |
-
)
|
| 94 |
|
| 95 |
# ==============================
|
| 96 |
-
# UI Design
|
| 97 |
# ==============================
|
| 98 |
|
| 99 |
CUSTOM_CSS = """
|
|
@@ -110,7 +97,7 @@ header {
|
|
| 110 |
}
|
| 111 |
"""
|
| 112 |
|
| 113 |
-
with gr.Blocks(
|
| 114 |
gr.Markdown(
|
| 115 |
"<h1 style='text-align:center;'>💻 CODEK AI</h1>"
|
| 116 |
"<p style='text-align:center;'>مساعد برمجي ذكي لاستخراج الأكواد ومعاينتها</p>"
|
|
@@ -118,7 +105,7 @@ with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
| 118 |
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column(scale=2):
|
| 121 |
-
chatbot = gr.Chatbot(
|
| 122 |
user_input = gr.Textbox(
|
| 123 |
placeholder="اكتب طلبك البرمجي هنا...",
|
| 124 |
lines=3
|
|
@@ -144,4 +131,4 @@ with gr.Blocks(css=CUSTOM_CSS) as demo:
|
|
| 144 |
# ==============================
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
-
demo.launch()
|
|
|
|
| 8 |
# ==============================
|
| 9 |
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 11 |
if not HF_TOKEN:
|
| 12 |
raise EnvironmentError("HF_TOKEN is not set in environment variables.")
|
| 13 |
|
|
|
|
| 49 |
return file_path
|
| 50 |
|
| 51 |
# ==============================
|
| 52 |
+
# Chat Logic (Gradio 6)
|
| 53 |
# ==============================
|
| 54 |
|
| 55 |
def chat_stream(user_message, history):
|
| 56 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 57 |
+
|
| 58 |
+
for user, assistant in history:
|
| 59 |
+
messages.append({"role": "user", "content": user})
|
| 60 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 61 |
+
|
| 62 |
messages.append({"role": "user", "content": user_message})
|
| 63 |
|
| 64 |
assistant_text = ""
|
|
|
|
| 72 |
for chunk in stream:
|
| 73 |
delta = chunk.choices[0].delta.content or ""
|
| 74 |
assistant_text += delta
|
| 75 |
+
yield history + [(user_message, assistant_text)], "", None, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
extracted_code = extract_code(assistant_text)
|
| 78 |
file_path = save_code_to_file(extracted_code) if extracted_code else None
|
| 79 |
|
| 80 |
+
yield history + [(user_message, assistant_text)], extracted_code, file_path, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# ==============================
|
| 83 |
+
# UI Design
|
| 84 |
# ==============================
|
| 85 |
|
| 86 |
CUSTOM_CSS = """
|
|
|
|
| 97 |
}
|
| 98 |
"""
|
| 99 |
|
| 100 |
+
with gr.Blocks() as demo:
|
| 101 |
gr.Markdown(
|
| 102 |
"<h1 style='text-align:center;'>💻 CODEK AI</h1>"
|
| 103 |
"<p style='text-align:center;'>مساعد برمجي ذكي لاستخراج الأكواد ومعاينتها</p>"
|
|
|
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=2):
|
| 108 |
+
chatbot = gr.Chatbot(height=500)
|
| 109 |
user_input = gr.Textbox(
|
| 110 |
placeholder="اكتب طلبك البرمجي هنا...",
|
| 111 |
lines=3
|
|
|
|
| 131 |
# ==============================
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
+
demo.launch(css=CUSTOM_CSS)
|