Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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)":
|
| 18 |
+
model_id = MODEL_GLM
|
| 19 |
+
else:
|
| 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, # Precise code
|
| 40 |
+
top_p=0.9,
|
| 41 |
+
stream=True,
|
| 42 |
+
details=True,
|
| 43 |
+
stop_sequences=["User:", "System:", "<|endoftext|>"]
|
| 44 |
+
)
|
| 45 |
+
|
| 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 |
+
error_msg = f"Error: The Free API is currently overloaded for {model_id}. \n\nTechnical details: {str(e)}"
|
| 56 |
+
yield error_msg
|
| 57 |
+
|
| 58 |
+
# --- The UI ---
|
| 59 |
+
with gr.Blocks(theme="soft") 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 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|