Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ if not HF_TOKEN:
|
|
| 12 |
raise EnvironmentError("HF_TOKEN is not set in environment variables.")
|
| 13 |
|
| 14 |
client = InferenceClient(
|
| 15 |
-
model="Qwen/Qwen2.5-Coder-
|
| 16 |
token=HF_TOKEN
|
| 17 |
)
|
| 18 |
|
|
@@ -39,15 +39,12 @@ def save_code_to_file(code: str) -> str:
|
|
| 39 |
return path
|
| 40 |
|
| 41 |
# ==============================
|
| 42 |
-
# Streaming Chat (Messages
|
| 43 |
# ==============================
|
| 44 |
|
| 45 |
def chat_stream(user_message, history):
|
| 46 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 47 |
-
|
| 48 |
-
for msg in history:
|
| 49 |
-
messages.append(msg)
|
| 50 |
-
|
| 51 |
messages.append({"role": "user", "content": user_message})
|
| 52 |
|
| 53 |
assistant_text = ""
|
|
@@ -57,7 +54,7 @@ def chat_stream(user_message, history):
|
|
| 57 |
stream = client.chat_completion(
|
| 58 |
messages=messages,
|
| 59 |
stream=True,
|
| 60 |
-
max_tokens=
|
| 61 |
)
|
| 62 |
|
| 63 |
for chunk in stream:
|
|
@@ -78,7 +75,7 @@ def chat_stream(user_message, history):
|
|
| 78 |
)
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
-
error_msg = f"❌ خطأ
|
| 82 |
yield (
|
| 83 |
new_history + [{"role": "assistant", "content": error_msg}],
|
| 84 |
"",
|
|
@@ -98,7 +95,7 @@ def chat_stream(user_message, history):
|
|
| 98 |
)
|
| 99 |
|
| 100 |
# ==============================
|
| 101 |
-
# Legendary Animated UI
|
| 102 |
# ==============================
|
| 103 |
|
| 104 |
CUSTOM_CSS = """
|
|
@@ -167,7 +164,11 @@ with gr.Blocks() as demo:
|
|
| 167 |
submit = gr.Button("🚀 Generate")
|
| 168 |
|
| 169 |
with gr.Column(scale=1):
|
| 170 |
-
code_preview = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
file_download = gr.File(label="⬇ Download Code")
|
| 172 |
|
| 173 |
submit.click(
|
|
@@ -177,8 +178,13 @@ with gr.Blocks() as demo:
|
|
| 177 |
)
|
| 178 |
|
| 179 |
# ==============================
|
| 180 |
-
# Run
|
| 181 |
# ==============================
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
| 184 |
-
demo.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
raise EnvironmentError("HF_TOKEN is not set in environment variables.")
|
| 13 |
|
| 14 |
client = InferenceClient(
|
| 15 |
+
model="Qwen/Qwen2.5-Coder-7B-Instruct",
|
| 16 |
token=HF_TOKEN
|
| 17 |
)
|
| 18 |
|
|
|
|
| 39 |
return path
|
| 40 |
|
| 41 |
# ==============================
|
| 42 |
+
# Streaming Chat Logic (Messages)
|
| 43 |
# ==============================
|
| 44 |
|
| 45 |
def chat_stream(user_message, history):
|
| 46 |
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
|
| 47 |
+
messages.extend(history)
|
|
|
|
|
|
|
|
|
|
| 48 |
messages.append({"role": "user", "content": user_message})
|
| 49 |
|
| 50 |
assistant_text = ""
|
|
|
|
| 54 |
stream = client.chat_completion(
|
| 55 |
messages=messages,
|
| 56 |
stream=True,
|
| 57 |
+
max_tokens=2048
|
| 58 |
)
|
| 59 |
|
| 60 |
for chunk in stream:
|
|
|
|
| 75 |
)
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
+
error_msg = f"❌ حصل خطأ:\n{str(e)}"
|
| 79 |
yield (
|
| 80 |
new_history + [{"role": "assistant", "content": error_msg}],
|
| 81 |
"",
|
|
|
|
| 95 |
)
|
| 96 |
|
| 97 |
# ==============================
|
| 98 |
+
# Legendary Animated UI (CSS)
|
| 99 |
# ==============================
|
| 100 |
|
| 101 |
CUSTOM_CSS = """
|
|
|
|
| 164 |
submit = gr.Button("🚀 Generate")
|
| 165 |
|
| 166 |
with gr.Column(scale=1):
|
| 167 |
+
code_preview = gr.Code(
|
| 168 |
+
label="⚙ Code Preview",
|
| 169 |
+
language="python",
|
| 170 |
+
lines=28
|
| 171 |
+
)
|
| 172 |
file_download = gr.File(label="⬇ Download Code")
|
| 173 |
|
| 174 |
submit.click(
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
# ==============================
|
| 181 |
+
# Run App
|
| 182 |
# ==============================
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
+
demo.queue(concurrency_count=2)
|
| 186 |
+
demo.launch(
|
| 187 |
+
css=CUSTOM_CSS,
|
| 188 |
+
server_name="0.0.0.0",
|
| 189 |
+
server_port=7860
|
| 190 |
+
)
|