w1r4 commited on
Commit
88644cf
·
verified ·
1 Parent(s): 10bc714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -2,7 +2,6 @@ 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",
@@ -12,10 +11,10 @@ MODELS = {
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-32B-Instruct")
20
 
21
  client = InferenceClient()
@@ -23,16 +22,22 @@ def generate_abap(message, history, model_choice):
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
 
@@ -41,7 +46,7 @@ def generate_abap(message, history, model_choice):
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
@@ -54,14 +59,13 @@ def generate_abap(message, history, model_choice):
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 32B (Recommended)",
 
2
  from huggingface_hub import InferenceClient
3
 
4
  # --- Configuration: Model List ---
 
5
  MODELS = {
6
  "Qwen 2.5 Coder 32B (Recommended)": "Qwen/Qwen2.5-Coder-32B-Instruct",
7
  "Llama 3.1 8B (Best Logic)": "meta-llama/Meta-Llama-3.1-8B-Instruct",
 
11
  }
12
 
13
  # Configuration for Memory
14
+ MAX_HISTORY = 10
15
 
16
  def generate_abap(message, history, model_choice):
17
+ # 1. Get the Hugging Face Model ID
18
  model_id = MODELS.get(model_choice, "Qwen/Qwen2.5-Coder-32B-Instruct")
19
 
20
  client = InferenceClient()
 
22
  system_prompt = "You are an expert SAP ABAP Developer. Write modern, efficient ABAP 7.4+ code. Always use inline declarations."
23
  messages = [{"role": "system", "content": system_prompt}]
24
 
25
+ # 2. Add History (Robust Fix)
26
+ # We slice the history to keep memory usage low
27
+ recent_history = history[-MAX_HISTORY:]
28
 
29
  for turn in recent_history:
30
+ # CASE 1: History is a List of Lists (Standard Gradio format: [[user, bot], ...])
31
+ if isinstance(turn, (list, tuple)):
32
+ messages.append({"role": "user", "content": str(turn[0])})
33
+ if len(turn) > 1 and turn[1] is not None:
34
+ messages.append({"role": "assistant", "content": str(turn[1])})
35
+
36
+ # CASE 2: History is a List of Dictionaries (Newer format: [{'role': 'user', ...}])
37
+ elif isinstance(turn, dict):
38
+ # We can simply append the dictionary directly if it has 'role' and 'content'
39
+ messages.append(turn)
40
+
41
  # 3. Add Current Message
42
  messages.append({"role": "user", "content": str(message)})
43
 
 
46
  stream = client.chat_completion(
47
  model=model_id,
48
  messages=messages,
49
+ max_tokens=2048,
50
  temperature=0.1,
51
  top_p=0.9,
52
  stream=True
 
59
  yield partial_message
60
 
61
  except Exception as e:
62
+ yield f"Error: The Free API is overloaded or model is too large. \nDetails: {str(e)}"
63
 
64
  # --- The UI ---
65
+ with gr.Blocks(theme="soft") as demo:
66
  gr.Markdown("# 🚀 ABAP Coder Multi-Model")
67
+ gr.Markdown("Select a model below. **Note:** Qwen 32B is large and may timeout on the free tier. If it fails, try Llama 3.1 8B.")
68
 
 
69
  model_selector = gr.Dropdown(
70
  choices=list(MODELS.keys()),
71
  value="Qwen 2.5 Coder 32B (Recommended)",