w1r4 commited on
Commit
c4d5905
·
verified ·
1 Parent(s): a81100f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -12,37 +12,43 @@ def generate_abap(message, history, model_choice):
12
  else:
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,
34
- stop_sequences=["User:", "System:", "<|endoftext|>"]
35
  )
36
 
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:
@@ -58,8 +64,6 @@ with gr.Blocks( ) as demo:
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)"],
 
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 history
24
+ for user_msg, bot_msg in history:
25
+ messages.append({"role": "user", "content": user_msg})
26
+ messages.append({"role": "assistant", "content": bot_msg})
27
+
28
+ # Add current message
29
+ messages.append({"role": "user", "content": message})
30
 
31
  try:
32
+ # --- FIX: Use chat_completion (Conversational API) ---
33
+ stream = client.chat_completion(
34
+ model=model_id,
35
+ messages=messages,
36
+ max_tokens=1024,
37
  temperature=0.1,
38
  top_p=0.9,
39
+ stream=True
 
 
40
  )
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
+ content = chunk.choices[0].delta.content
47
+ partial_message += content
48
  yield partial_message
49
 
50
  except Exception as e:
51
+ yield f"Error: The Free API provider rejected the request. \n\nDetails: {str(e)}"
52
 
53
  # --- The UI ---
54
  with gr.Blocks( ) as demo:
 
64
  chat = gr.ChatInterface(
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).", "Qwen 2.5 Coder (Recommended)"],