Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,9 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
# --- Configuration ---
|
| 5 |
-
# OPTION 1: Qwen 2.5 Coder (Recommended: Faster, Smarter)
|
| 6 |
MODEL_QWEN = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 7 |
-
|
| 8 |
-
# OPTION 2: CodeGeeX4 (GLM-4 Architecture)
|
| 9 |
-
# Note: This might timeout more often on the free tier because it's larger (9B)
|
| 10 |
MODEL_GLM = "THUDM/codegeex4-all-9b"
|
| 11 |
|
| 12 |
-
# We default to Qwen because it is more reliable on the Free API
|
| 13 |
-
CURRENT_MODEL = MODEL_QWEN
|
| 14 |
-
|
| 15 |
def generate_abap(message, history, model_choice):
|
| 16 |
# Select the model based on user dropdown
|
| 17 |
if model_choice == "GLM-4 (CodeGeeX4)":
|
|
@@ -20,23 +13,21 @@ def generate_abap(message, history, model_choice):
|
|
| 20 |
model_id = MODEL_QWEN
|
| 21 |
|
| 22 |
client = InferenceClient(model=model_id)
|
| 23 |
-
|
| 24 |
# System Prompt specialized for ABAP
|
| 25 |
system_prompt = "You are an expert SAP ABAP Developer. Write modern, efficient ABAP 7.4+ code. Always use inline declarations."
|
| 26 |
|
| 27 |
# Construct Prompt
|
| 28 |
-
# We use a generic chat format that works for both models
|
| 29 |
prompt = f"System: {system_prompt}\n"
|
| 30 |
for user, bot in history:
|
| 31 |
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 32 |
prompt += f"User: {message}\nAssistant:"
|
| 33 |
|
| 34 |
try:
|
| 35 |
-
# Stream response from HF GPU
|
| 36 |
stream = client.text_generation(
|
| 37 |
prompt,
|
| 38 |
max_new_tokens=1024,
|
| 39 |
-
temperature=0.1,
|
| 40 |
top_p=0.9,
|
| 41 |
stream=True,
|
| 42 |
details=True,
|
|
@@ -46,35 +37,33 @@ def generate_abap(message, history, model_choice):
|
|
| 46 |
partial_message = ""
|
| 47 |
for response in stream:
|
| 48 |
token = response.token.text
|
| 49 |
-
# Filter out stop tokens
|
| 50 |
if token not in ["User:", "System:"]:
|
| 51 |
partial_message += token
|
| 52 |
yield partial_message
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
yield error_msg
|
| 57 |
|
| 58 |
# --- The UI ---
|
| 59 |
with gr.Blocks( ) as demo:
|
| 60 |
gr.Markdown("# 🚀 ABAP Coder (Serverless GPU)")
|
| 61 |
gr.Markdown("Generate ABAP code using top open-source models running on Hugging Face's Free API.")
|
| 62 |
|
| 63 |
-
# Dropdown to choose model
|
| 64 |
model_selector = gr.Dropdown(
|
| 65 |
choices=["Qwen 2.5 Coder (Recommended)", "GLM-4 (CodeGeeX4)"],
|
| 66 |
value="Qwen 2.5 Coder (Recommended)",
|
| 67 |
label="Select AI Model"
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# Chat Interface
|
| 71 |
chat = gr.ChatInterface(
|
| 72 |
fn=generate_abap,
|
| 73 |
additional_inputs=[model_selector],
|
|
|
|
|
|
|
| 74 |
examples=[
|
| 75 |
-
"Write a report to select data from MARA using inline declarations.",
|
| 76 |
-
"Create a CDS View for Sales Orders (VBAK/VBAP).",
|
| 77 |
-
"Explain how to use READ TABLE with ASSIGNING FIELD-SYMBOL."
|
| 78 |
]
|
| 79 |
)
|
| 80 |
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
# --- Configuration ---
|
|
|
|
| 5 |
MODEL_QWEN = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
|
|
|
|
|
|
|
|
|
| 6 |
MODEL_GLM = "THUDM/codegeex4-all-9b"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def generate_abap(message, history, model_choice):
|
| 9 |
# Select the model based on user dropdown
|
| 10 |
if model_choice == "GLM-4 (CodeGeeX4)":
|
|
|
|
| 13 |
model_id = MODEL_QWEN
|
| 14 |
|
| 15 |
client = InferenceClient(model=model_id)
|
| 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 |
# Construct Prompt
|
|
|
|
| 21 |
prompt = f"System: {system_prompt}\n"
|
| 22 |
for user, bot in history:
|
| 23 |
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 24 |
prompt += f"User: {message}\nAssistant:"
|
| 25 |
|
| 26 |
try:
|
|
|
|
| 27 |
stream = client.text_generation(
|
| 28 |
prompt,
|
| 29 |
max_new_tokens=1024,
|
| 30 |
+
temperature=0.1,
|
| 31 |
top_p=0.9,
|
| 32 |
stream=True,
|
| 33 |
details=True,
|
|
|
|
| 37 |
partial_message = ""
|
| 38 |
for response in stream:
|
| 39 |
token = response.token.text
|
|
|
|
| 40 |
if token not in ["User:", "System:"]:
|
| 41 |
partial_message += token
|
| 42 |
yield partial_message
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
+
yield f"Error: The Free API is overloaded. Details: {str(e)}"
|
|
|
|
| 46 |
|
| 47 |
# --- The UI ---
|
| 48 |
with gr.Blocks( ) as demo:
|
| 49 |
gr.Markdown("# 🚀 ABAP Coder (Serverless GPU)")
|
| 50 |
gr.Markdown("Generate ABAP code using top open-source models running on Hugging Face's Free API.")
|
| 51 |
|
|
|
|
| 52 |
model_selector = gr.Dropdown(
|
| 53 |
choices=["Qwen 2.5 Coder (Recommended)", "GLM-4 (CodeGeeX4)"],
|
| 54 |
value="Qwen 2.5 Coder (Recommended)",
|
| 55 |
label="Select AI Model"
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
chat = gr.ChatInterface(
|
| 59 |
fn=generate_abap,
|
| 60 |
additional_inputs=[model_selector],
|
| 61 |
+
# --- THE FIX IS HERE ---
|
| 62 |
+
# Each example is now a list: [Question, Dropdown Value]
|
| 63 |
examples=[
|
| 64 |
+
["Write a report to select data from MARA using inline declarations.", "Qwen 2.5 Coder (Recommended)"],
|
| 65 |
+
["Create a CDS View for Sales Orders (VBAK/VBAP).", "Qwen 2.5 Coder (Recommended)"],
|
| 66 |
+
["Explain how to use READ TABLE with ASSIGNING FIELD-SYMBOL.", "Qwen 2.5 Coder (Recommended)"]
|
| 67 |
]
|
| 68 |
)
|
| 69 |
|