anaspro
commited on
Commit
·
fbd6279
1
Parent(s):
8bda143
- app.py +18 -57
- system_prompt.txt +1 -1
app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
-
import os
|
| 2 |
from transformers import pipeline, TextIteratorStreamer
|
| 3 |
from threading import Thread
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
| 6 |
import re
|
| 7 |
-
from huggingface_hub import login
|
| 8 |
-
import logging
|
| 9 |
from openai_harmony import (
|
| 10 |
load_harmony_encoding,
|
| 11 |
HarmonyEncodingName,
|
|
@@ -17,29 +14,12 @@ from openai_harmony import (
|
|
| 17 |
ReasoningEffort,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Setup logging
|
| 21 |
-
logging.basicConfig(level=logging.INFO)
|
| 22 |
-
logger = logging.getLogger(__name__)
|
| 23 |
-
|
| 24 |
-
# Login to Hugging Face
|
| 25 |
-
if os.getenv("HF_TOKEN"):
|
| 26 |
-
login(token=os.getenv("HF_TOKEN"))
|
| 27 |
-
logger.info("🔐 Logged in to Hugging Face")
|
| 28 |
-
|
| 29 |
# regex config
|
| 30 |
RE_REASONING = re.compile(r'(?i)Reasoning:\s*(low|medium|high)')
|
| 31 |
RE_FINAL_MARKER = re.compile(r'(?i)assistantfinal')
|
| 32 |
RE_ANALYSIS_PREFIX = re.compile(r'(?i)^analysis\s*')
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
try:
|
| 36 |
-
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 37 |
-
DEFAULT_SYSTEM_PROMPT = f.read()
|
| 38 |
-
except FileNotFoundError:
|
| 39 |
-
logger.warning("system_prompt.txt not found, using default prompt")
|
| 40 |
-
DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant. Reasoning: medium"
|
| 41 |
-
|
| 42 |
-
# Parse reasoning level from system prompt
|
| 43 |
def parse_reasoning_and_instructions(system_prompt: str):
|
| 44 |
instructions = system_prompt or "You are a helpful assistant."
|
| 45 |
match = RE_REASONING.search(instructions)
|
|
@@ -52,7 +32,7 @@ def parse_reasoning_and_instructions(system_prompt: str):
|
|
| 52 |
cleaned_instructions = RE_REASONING.sub('', instructions).strip()
|
| 53 |
return effort, cleaned_instructions
|
| 54 |
|
| 55 |
-
model_id = "
|
| 56 |
|
| 57 |
pipe = pipeline(
|
| 58 |
"text-generation",
|
|
@@ -62,7 +42,6 @@ pipe = pipeline(
|
|
| 62 |
trust_remote_code=True,
|
| 63 |
)
|
| 64 |
enc = load_harmony_encoding(HarmonyEncodingName.HARMONY_GPT_OSS)
|
| 65 |
-
|
| 66 |
def format_conversation_history(chat_history):
|
| 67 |
messages = []
|
| 68 |
for item in chat_history:
|
|
@@ -70,7 +49,7 @@ def format_conversation_history(chat_history):
|
|
| 70 |
content = item["content"]
|
| 71 |
if isinstance(content, list):
|
| 72 |
content = content[0]["text"] if content and "text" in content[0] else str(content)
|
| 73 |
-
messages.append({"role":
|
| 74 |
return messages
|
| 75 |
|
| 76 |
@spaces.GPU()
|
|
@@ -122,7 +101,7 @@ def generate_response(input_data, chat_history, max_new_tokens, system_prompt, t
|
|
| 122 |
final += chunk
|
| 123 |
clean_thinking = RE_ANALYSIS_PREFIX.sub('', thinking).strip()
|
| 124 |
clean_final = final.strip()
|
| 125 |
-
formatted = f"<details open><summary
|
| 126 |
yield formatted
|
| 127 |
|
| 128 |
demo = gr.ChatInterface(
|
|
@@ -131,9 +110,9 @@ demo = gr.ChatInterface(
|
|
| 131 |
gr.Slider(label="Max new tokens", minimum=64, maximum=4096, step=1, value=2048),
|
| 132 |
gr.Textbox(
|
| 133 |
label="System Prompt",
|
| 134 |
-
value=
|
| 135 |
-
lines=
|
| 136 |
-
placeholder="
|
| 137 |
),
|
| 138 |
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
|
| 139 |
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
|
@@ -141,42 +120,24 @@ demo = gr.ChatInterface(
|
|
| 141 |
gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
|
| 142 |
],
|
| 143 |
examples=[
|
| 144 |
-
[{"text": "
|
| 145 |
-
[{"text": "
|
| 146 |
-
[{"text": "
|
| 147 |
-
|
| 148 |
-
[{"text": "أنا صاحب مؤسسة، أي باقة تناسب 10 موظفين؟ حلل الاحتياجات والتكلفة."}],
|
| 149 |
-
[{"text": "شلون اغير كلمة مرور الواي فاي خطوة بخطوة؟"}],
|
| 150 |
-
[{"text": "النت ينقطع فجأة ويعود. حلل السبب واعطني حل شامل."}],
|
| 151 |
],
|
| 152 |
cache_examples=False,
|
| 153 |
type="messages",
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
✨ **قدرات متقدمة:**
|
| 158 |
-
- 🧠 تفكير منطقي عميق (Chain-of-Thought)
|
| 159 |
-
- 📊 حلول خطوة بخطوة مع التحليل
|
| 160 |
-
- 🎯 مستويات تفكير قابلة للتعديل (Reasoning: low/medium/high)
|
| 161 |
-
- 💬 دعم كامل للغة العربية العراقية
|
| 162 |
-
- 🔧 تشخيص وحلول متقدمة للمشاكل التقنية
|
| 163 |
-
|
| 164 |
-
**احجي معاه كأنك زبون:** اشرح مشكلتك، اسأل عن الباقات، او اطلب تذكرة دعم.
|
| 165 |
-
|
| 166 |
-
*يمكنك رؤية عملية التفكير (Thinking Process) عند النقر على السهم أعلى الإجابة.*""",
|
| 167 |
fill_height=True,
|
| 168 |
textbox=gr.Textbox(
|
| 169 |
-
label="
|
| 170 |
-
placeholder="
|
| 171 |
),
|
| 172 |
-
stop_btn="
|
| 173 |
multimodal=False,
|
| 174 |
-
theme=gr.themes.Soft(
|
| 175 |
-
primary_hue="blue",
|
| 176 |
-
secondary_hue="gray",
|
| 177 |
-
neutral_hue="slate"
|
| 178 |
-
),
|
| 179 |
)
|
| 180 |
|
| 181 |
if __name__ == "__main__":
|
| 182 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline, TextIteratorStreamer
|
| 2 |
from threading import Thread
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
import re
|
|
|
|
|
|
|
| 6 |
from openai_harmony import (
|
| 7 |
load_harmony_encoding,
|
| 8 |
HarmonyEncodingName,
|
|
|
|
| 14 |
ReasoningEffort,
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# regex config
|
| 18 |
RE_REASONING = re.compile(r'(?i)Reasoning:\s*(low|medium|high)')
|
| 19 |
RE_FINAL_MARKER = re.compile(r'(?i)assistantfinal')
|
| 20 |
RE_ANALYSIS_PREFIX = re.compile(r'(?i)^analysis\s*')
|
| 21 |
|
| 22 |
+
# I think for system prompt reasoning level OpenAI mentioned you should do parsing so here's
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def parse_reasoning_and_instructions(system_prompt: str):
|
| 24 |
instructions = system_prompt or "You are a helpful assistant."
|
| 25 |
match = RE_REASONING.search(instructions)
|
|
|
|
| 32 |
cleaned_instructions = RE_REASONING.sub('', instructions).strip()
|
| 33 |
return effort, cleaned_instructions
|
| 34 |
|
| 35 |
+
model_id = "openai/gpt-oss-20b"
|
| 36 |
|
| 37 |
pipe = pipeline(
|
| 38 |
"text-generation",
|
|
|
|
| 42 |
trust_remote_code=True,
|
| 43 |
)
|
| 44 |
enc = load_harmony_encoding(HarmonyEncodingName.HARMONY_GPT_OSS)
|
|
|
|
| 45 |
def format_conversation_history(chat_history):
|
| 46 |
messages = []
|
| 47 |
for item in chat_history:
|
|
|
|
| 49 |
content = item["content"]
|
| 50 |
if isinstance(content, list):
|
| 51 |
content = content[0]["text"] if content and "text" in content[0] else str(content)
|
| 52 |
+
messages.append({"role": role, "content": content})
|
| 53 |
return messages
|
| 54 |
|
| 55 |
@spaces.GPU()
|
|
|
|
| 101 |
final += chunk
|
| 102 |
clean_thinking = RE_ANALYSIS_PREFIX.sub('', thinking).strip()
|
| 103 |
clean_final = final.strip()
|
| 104 |
+
formatted = f"<details open><summary>Click to view Thinking Process</summary>\n\n{clean_thinking}\n\n</details>\n\n{clean_final}"
|
| 105 |
yield formatted
|
| 106 |
|
| 107 |
demo = gr.ChatInterface(
|
|
|
|
| 110 |
gr.Slider(label="Max new tokens", minimum=64, maximum=4096, step=1, value=2048),
|
| 111 |
gr.Textbox(
|
| 112 |
label="System Prompt",
|
| 113 |
+
value="You are a helpful assistant. Reasoning: medium",
|
| 114 |
+
lines=4,
|
| 115 |
+
placeholder="Change system prompt"
|
| 116 |
),
|
| 117 |
gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
|
| 118 |
gr.Slider(label="Top-p", minimum=0.05, maximum=1.0, step=0.05, value=0.9),
|
|
|
|
| 120 |
gr.Slider(label="Repetition Penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.0)
|
| 121 |
],
|
| 122 |
examples=[
|
| 123 |
+
[{"text": "Explain Newton laws clearly and concisely"}],
|
| 124 |
+
[{"text": "What are the benefits of open weight AI models"}],
|
| 125 |
+
[{"text": "Write a Python function to calculate the Fibonacci sequence"}],
|
| 126 |
+
|
|
|
|
|
|
|
|
|
|
| 127 |
],
|
| 128 |
cache_examples=False,
|
| 129 |
type="messages",
|
| 130 |
+
description="""# gpt-oss-20b Demo
|
| 131 |
+
Give it a couple of seconds to start. You can adjust reasoning level in the system prompt like "Reasoning: high." Click to view thinking process (default is on).""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
fill_height=True,
|
| 133 |
textbox=gr.Textbox(
|
| 134 |
+
label="Query Input",
|
| 135 |
+
placeholder="Type your prompt"
|
| 136 |
),
|
| 137 |
+
stop_btn="Stop Generation",
|
| 138 |
multimodal=False,
|
| 139 |
+
theme=gr.themes.Soft()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
demo.launch()
|
system_prompt.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
أنت مساعد ذكي متقدم يعتمد على نموذج GPT-OSS-20B من OpenAI مع دعم فني لشركة NB TEL.
|
| 2 |
تحجي بالعراقي بأسلوب مهني ومحترف.
|
| 3 |
|
| 4 |
-
Reasoning:
|
| 5 |
|
| 6 |
قواعد محدثة:
|
| 7 |
- أنت متخصص في دعم فني الإنترنت مع قدرات تفكير متقدمة
|
|
|
|
| 1 |
أنت مساعد ذكي متقدم يعتمد على نموذج GPT-OSS-20B من OpenAI مع دعم فني لشركة NB TEL.
|
| 2 |
تحجي بالعراقي بأسلوب مهني ومحترف.
|
| 3 |
|
| 4 |
+
Reasoning: medium - استخدم مستوى تفكير عالي للتحليل المتعمق والحلول المتقدمة.
|
| 5 |
|
| 6 |
قواعد محدثة:
|
| 7 |
- أنت متخصص في دعم فني الإنترنت مع قدرات تفكير متقدمة
|