Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# --- Configuration ---
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def generate_abap(message, history, model_choice):
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
else:
|
| 13 |
-
model_id = MODEL_QWEN
|
| 14 |
-
|
| 15 |
client = InferenceClient()
|
| 16 |
|
| 17 |
-
# System Prompt specialized for ABAP
|
| 18 |
system_prompt = "You are an expert SAP ABAP Developer. Write modern, efficient ABAP 7.4+ code. Always use inline declarations."
|
| 19 |
-
|
| 20 |
-
# --- FIX: Build structured messages for Chat API ---
|
| 21 |
messages = [{"role": "system", "content": system_prompt}]
|
| 22 |
|
| 23 |
-
# Add
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Add
|
| 29 |
-
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
try:
|
| 32 |
-
#
|
| 33 |
stream = client.chat_completion(
|
| 34 |
model=model_id,
|
| 35 |
messages=messages,
|
| 36 |
-
max_tokens=
|
| 37 |
temperature=0.1,
|
| 38 |
top_p=0.9,
|
| 39 |
stream=True
|
|
@@ -41,23 +49,22 @@ def generate_abap(message, history, model_choice):
|
|
| 41 |
|
| 42 |
partial_message = ""
|
| 43 |
for chunk in stream:
|
| 44 |
-
# Extract content from the stream delta
|
| 45 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 46 |
-
|
| 47 |
-
partial_message += content
|
| 48 |
yield partial_message
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
yield f"Error: The Free API
|
| 52 |
|
| 53 |
# --- The UI ---
|
| 54 |
with gr.Blocks( ) as demo:
|
| 55 |
-
gr.Markdown("# 🚀 ABAP Coder
|
| 56 |
-
gr.Markdown("
|
| 57 |
|
|
|
|
| 58 |
model_selector = gr.Dropdown(
|
| 59 |
-
choices=
|
| 60 |
-
value="Qwen 2.5 Coder (Recommended)",
|
| 61 |
label="Select AI Model"
|
| 62 |
)
|
| 63 |
|
|
@@ -65,9 +72,9 @@ with gr.Blocks( ) as demo:
|
|
| 65 |
fn=generate_abap,
|
| 66 |
additional_inputs=[model_selector],
|
| 67 |
examples=[
|
| 68 |
-
["Write a report to select data from MARA using inline declarations.", "Qwen 2.5 Coder (Recommended)"],
|
| 69 |
-
["Create a CDS View for Sales Orders (VBAK/VBAP).", "
|
| 70 |
-
["Explain how to use READ TABLE with ASSIGNING FIELD-SYMBOL.", "
|
| 71 |
]
|
| 72 |
)
|
| 73 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# --- Configuration: Model List ---
|
| 5 |
+
# We use a Dictionary to map "Friendly Names" to "Model IDs"
|
| 6 |
+
MODELS = {
|
| 7 |
+
"Qwen 2.5 Coder 32B (Recommended)": "Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 8 |
+
"Llama 3.1 8B (Best Logic)": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 9 |
+
"DeepSeek Coder V2 Lite (Expert)": "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct",
|
| 10 |
+
"Mistral Nemo 12B (Strong)": "mistralai/Mistral-Nemo-Instruct-2407",
|
| 11 |
+
"GLM-4 / CodeGeeX4 9B": "THUDM/codegeex4-all-9b"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Configuration for Memory
|
| 15 |
+
MAX_HISTORY = 5
|
| 16 |
|
| 17 |
def generate_abap(message, history, model_choice):
|
| 18 |
+
# 1. Get the Hugging Face Model ID from the dropdown selection
|
| 19 |
+
model_id = MODELS.get(model_choice, "Qwen/Qwen2.5-Coder-7B-Instruct")
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
| 21 |
client = InferenceClient()
|
| 22 |
|
|
|
|
| 23 |
system_prompt = "You are an expert SAP ABAP Developer. Write modern, efficient ABAP 7.4+ code. Always use inline declarations."
|
|
|
|
|
|
|
| 24 |
messages = [{"role": "system", "content": system_prompt}]
|
| 25 |
|
| 26 |
+
# 2. Add History (Sliding Window)
|
| 27 |
+
recent_history = history[-MAX_HISTORY:]
|
| 28 |
+
|
| 29 |
+
for turn in recent_history:
|
| 30 |
+
# Extract User and Bot messages safely
|
| 31 |
+
user_msg = turn[0]
|
| 32 |
+
bot_msg = turn[1]
|
| 33 |
+
messages.append({"role": "user", "content": str(user_msg)})
|
| 34 |
+
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 35 |
|
| 36 |
+
# 3. Add Current Message
|
| 37 |
+
messages.append({"role": "user", "content": str(message)})
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# 4. Stream Response
|
| 41 |
stream = client.chat_completion(
|
| 42 |
model=model_id,
|
| 43 |
messages=messages,
|
| 44 |
+
max_tokens=2048, # Increased token limit for longer code
|
| 45 |
temperature=0.1,
|
| 46 |
top_p=0.9,
|
| 47 |
stream=True
|
|
|
|
| 49 |
|
| 50 |
partial_message = ""
|
| 51 |
for chunk in stream:
|
|
|
|
| 52 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 53 |
+
partial_message += chunk.choices[0].delta.content
|
|
|
|
| 54 |
yield partial_message
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
+
yield f"Error: The Free API is overloaded for {model_choice}. Try switching to Qwen or Llama. \n\nDetails: {str(e)}"
|
| 58 |
|
| 59 |
# --- The UI ---
|
| 60 |
with gr.Blocks( ) as demo:
|
| 61 |
+
gr.Markdown("# 🚀 ABAP Coder Multi-Model")
|
| 62 |
+
gr.Markdown("Select a model below. If one gives an error, try another!")
|
| 63 |
|
| 64 |
+
# Dropdown with all our new models
|
| 65 |
model_selector = gr.Dropdown(
|
| 66 |
+
choices=list(MODELS.keys()),
|
| 67 |
+
value="Qwen 2.5 Coder 7B (Recommended)",
|
| 68 |
label="Select AI Model"
|
| 69 |
)
|
| 70 |
|
|
|
|
| 72 |
fn=generate_abap,
|
| 73 |
additional_inputs=[model_selector],
|
| 74 |
examples=[
|
| 75 |
+
["Write a report to select data from MARA using inline declarations.", "Qwen 2.5 Coder 7B (Recommended)"],
|
| 76 |
+
["Create a CDS View for Sales Orders (VBAK/VBAP).", "Llama 3.1 8B (Best Logic)"],
|
| 77 |
+
["Explain how to use READ TABLE with ASSIGNING FIELD-SYMBOL.", "DeepSeek Coder V2 Lite (Expert)"]
|
| 78 |
]
|
| 79 |
)
|
| 80 |
|