amkyawdev commited on
Commit
1bbc908
·
verified ·
1 Parent(s): d6a2ae9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -30
app.py CHANGED
@@ -9,7 +9,7 @@ from difflib import SequenceMatcher
9
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
10
  client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
11
 
12
- # --- ၁။ Dataset Loading (Safe Indexing) ---
13
  chat_db = []
14
  try:
15
  csv_url = "https://huggingface.co/datasets/amkyawdev/AmkyawDev-Dataset/raw/main/train.csv"
@@ -17,7 +17,6 @@ try:
17
  if not df.empty:
18
  for _, row in df.iterrows():
19
  if len(row) >= 2:
20
- # Column နာမည်တွေ လွဲနေရင်တောင် index နဲ့ ဆွဲထုတ်မယ်
21
  u_text = str(row.iloc[0]).strip()
22
  a_text = str(row.iloc[1]).strip()
23
  if u_text != "nan" and a_text != "nan":
@@ -26,36 +25,31 @@ try:
26
  except Exception as e:
27
  print(f"⚠️ Dataset Warning: {e}")
28
 
29
- # --- ၂။ AI Logic (Mixtral-8x7b) ---
30
  def generate_response(user_input, history):
31
- user_input = user_input.strip()
32
-
33
  # Step 1: Dataset Match
34
  for pair in chat_db:
35
- if SequenceMatcher(None, user_input, pair["u"]).ratio() > 0.85:
36
  res = pair["a"]
37
  for i in range(len(res)):
38
  time.sleep(0.005)
39
  yield res[: i + 1]
40
  return
41
 
42
- # Step 2: Groq API
43
  if client:
44
  try:
45
- # Mixtral 8x7b ကို သုံးထားတယ် (Natural ဖြစ်စေဖို့)
46
- messages = [{"role": "system", "content": "You are a friendly Myanmar AI assistant."}]
47
-
48
- # History format ကို OpenAI style ပြောင်းပေးခြင်း
49
  for h in history:
50
  if h[0]: messages.append({"role": "user", "content": h[0]})
51
  if h[1]: messages.append({"role": "assistant", "content": h[1]})
52
-
53
  messages.append({"role": "user", "content": user_input})
54
 
55
  stream = client.chat.completions.create(
56
  messages=messages,
57
- model="llama-3.1-8b-instant",
58
- temperature=0.8,
59
  stream=True,
60
  )
61
 
@@ -65,61 +59,62 @@ def generate_response(user_input, history):
65
  full_res += chunk.choices[0].delta.content
66
  yield full_res
67
  return
68
- except Exception:
69
- pass
70
 
71
- yield "တောင်းပန်ပါတယ်၊ အဖြရှာမတွပါဘူးခင်ဗျာ။"
72
 
73
- # --- ၃။ UI Design ---
74
  custom_css = """
75
  footer { visibility: hidden !important; }
76
- .gradio-container { background: #0d1117 !important; color: white !important; }
77
  .header-card {
78
  background: rgba(255, 255, 255, 0.03);
79
  border: 1px solid rgba(255, 255, 255, 0.1);
80
- border-radius: 15px; padding: 15px; margin-bottom: 20px;
81
  }
82
  #input-row {
83
- background: #161b22 !important; border-radius: 30px !important;
84
  border: 1px solid #30363d !important; padding: 5px 15px !important;
 
85
  }
86
  #input-box textarea { background: transparent !important; color: white !important; border: none !important; }
87
  #send-btn {
88
  background: #ffffff !important; color: #000000 !important;
89
- border-radius: 50% !important; width: 42px !important; height: 42px !important;
 
90
  }
 
91
  """
92
 
93
  with gr.Blocks(title="Amkyaw AI v4") as demo:
94
  gr.HTML("""
95
  <div class="header-card">
96
  <div style="display:flex; justify-content: space-between; align-items: center;">
97
- <div><b style="color: #58a6ff; font-size: 20px;">Amkyaw AI</b></div>
98
- <div style="color:#8b949e; font-size:12px;">Mixtral-8x7B | Stable UI</div>
99
  </div>
100
  </div>
101
  """)
102
 
103
- # type="messages" ကို ဖြုတ်လိုက်ပါပြီ (TypeError ရှင်းဖို့)
104
- chatbot = gr.Chatbot(show_label=False, height=600)
105
 
106
  with gr.Row(elem_id="input-row"):
107
- msg = gr.Textbox(placeholder="Ask me anything...", show_label=False, scale=15, elem_id="input-box", container=False)
108
  submit = gr.Button("↑", elem_id="send-btn", scale=1)
109
 
110
  def user_flow(user_msg, history):
111
  if not user_msg: return "", history
112
- # Format ဟောင်း [[user, bot]] အတိုင်း ပြန်သုံးပါတယ်
113
  return "", history + [[user_msg, None]]
114
 
115
  def bot_flow(history):
116
  user_input = history[-1][0]
117
- history[-1][1] = ""
118
- # generate_response ကို history အဟောင်းတွေပါ ပေးပို့တယ်
119
  for chunk in generate_response(user_input, history[:-1]):
120
  history[-1][1] = chunk
121
  yield history
122
 
 
123
  msg.submit(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
124
  submit.click(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
125
 
 
9
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
10
  client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
11
 
12
+ # --- ၁။ Dataset Loading ---
13
  chat_db = []
14
  try:
15
  csv_url = "https://huggingface.co/datasets/amkyawdev/AmkyawDev-Dataset/raw/main/train.csv"
 
17
  if not df.empty:
18
  for _, row in df.iterrows():
19
  if len(row) >= 2:
 
20
  u_text = str(row.iloc[0]).strip()
21
  a_text = str(row.iloc[1]).strip()
22
  if u_text != "nan" and a_text != "nan":
 
25
  except Exception as e:
26
  print(f"⚠️ Dataset Warning: {e}")
27
 
28
+ # --- ၂။ Core AI Logic (Llama-3.1-8b-instant) ---
29
  def generate_response(user_input, history):
 
 
30
  # Step 1: Dataset Match
31
  for pair in chat_db:
32
+ if SequenceMatcher(None, user_input.lower(), pair["u"].lower()).ratio() > 0.85:
33
  res = pair["a"]
34
  for i in range(len(res)):
35
  time.sleep(0.005)
36
  yield res[: i + 1]
37
  return
38
 
39
+ # Step 2: Groq API (Using Llama-3.1-8b-instant)
40
  if client:
41
  try:
42
+ messages = [{"role": "system", "content": "You are Amkyaw AI, a helpful Myanmar assistant. Answer clearly in Unicode."}]
43
+ # Convert history to OpenAI format
 
 
44
  for h in history:
45
  if h[0]: messages.append({"role": "user", "content": h[0]})
46
  if h[1]: messages.append({"role": "assistant", "content": h[1]})
 
47
  messages.append({"role": "user", "content": user_input})
48
 
49
  stream = client.chat.completions.create(
50
  messages=messages,
51
+ model="llama-3.1-8b-instant", # ပြန်ပြောင်းလိုက်တဲ့ Model ID
52
+ temperature=0.7,
53
  stream=True,
54
  )
55
 
 
59
  full_res += chunk.choices[0].delta.content
60
  yield full_res
61
  return
62
+ except Exception as e:
63
+ print(f"Groq Error: {e}")
64
 
65
+ yield "တောင်းပန်ပါတယ်၊ အခုလောလောဆယ် ဖြနိုင်သေးပါဘူးခင်ဗျာ။"
66
 
67
+ # --- ၃။ Professional Modern UI (CSS) ---
68
  custom_css = """
69
  footer { visibility: hidden !important; }
70
+ .gradio-container { background: #0d1117 !important; color: white !important; font-family: sans-serif; }
71
  .header-card {
72
  background: rgba(255, 255, 255, 0.03);
73
  border: 1px solid rgba(255, 255, 255, 0.1);
74
+ border-radius: 12px; padding: 15px 20px; margin-bottom: 20px;
75
  }
76
  #input-row {
77
+ background: #161b22 !important; border-radius: 25px !important;
78
  border: 1px solid #30363d !important; padding: 5px 15px !important;
79
+ box-shadow: 0 4px 10px rgba(0,0,0,0.2);
80
  }
81
  #input-box textarea { background: transparent !important; color: white !important; border: none !important; }
82
  #send-btn {
83
  background: #ffffff !important; color: #000000 !important;
84
+ border-radius: 50% !important; width: 40px !important; height: 40px !important; min-width: 40px !important;
85
+ font-weight: bold; cursor: pointer; transition: 0.2s;
86
  }
87
+ #send-btn:hover { transform: scale(1.1); }
88
  """
89
 
90
  with gr.Blocks(title="Amkyaw AI v4") as demo:
91
  gr.HTML("""
92
  <div class="header-card">
93
  <div style="display:flex; justify-content: space-between; align-items: center;">
94
+ <b style="color: #58a6ff; font-size: 19px;">Amkyaw AI</b>
95
+ <span style="color:#8b949e; font-size:12px;">Llama 3.1 8B | Instant Speed</span>
96
  </div>
97
  </div>
98
  """)
99
 
100
+ chatbot = gr.Chatbot(show_label=False, height=580)
 
101
 
102
  with gr.Row(elem_id="input-row"):
103
+ msg = gr.Textbox(placeholder="Type your message...", show_label=False, scale=15, elem_id="input-box", container=False)
104
  submit = gr.Button("↑", elem_id="send-btn", scale=1)
105
 
106
  def user_flow(user_msg, history):
107
  if not user_msg: return "", history
 
108
  return "", history + [[user_msg, None]]
109
 
110
  def bot_flow(history):
111
  user_input = history[-1][0]
112
+ # Streaming response back to UI
 
113
  for chunk in generate_response(user_input, history[:-1]):
114
  history[-1][1] = chunk
115
  yield history
116
 
117
+ # Event sequence
118
  msg.submit(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
119
  submit.click(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
120