Spaces:
Sleeping
Sleeping
File size: 4,652 Bytes
96dbee3 5247507 9f1c159 3ab5923 aac6e16 4b5feaf 431279d aac6e16 6b86e52 aac6e16 553917f fb0754f 3ab5923 31c982c 3ab5923 aac6e16 fb0754f aac6e16 895f7dc aac6e16 d6a2ae9 aac6e16 5247507 1bbc908 3ab5923 553917f 3ab5923 aac6e16 332a325 aac6e16 31c982c aac6e16 3ab5923 aac6e16 3ab5923 aac6e16 3ab5923 3465c0b 3ab5923 3465c0b 3ab5923 1bbc908 3ab5923 aac6e16 3ab5923 3465c0b 3ab5923 3465c0b aac6e16 3ab5923 09ca0c6 3465c0b 09ca0c6 3ab5923 82f4397 3ab5923 aac6e16 3ab5923 d6a2ae9 3ab5923 d6a2ae9 aac6e16 3ab5923 d6a2ae9 3ab5923 aac6e16 3ab5923 5400081 3ab5923 aac6e16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | import gradio as gr
import pandas as pd
import os
import time
from groq import Groq
from difflib import SequenceMatcher
# API Key Configuration
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
client = Groq(api_key=GROQ_API_KEY) if GROQ_API_KEY else None
# --- αα Dataset Loading (Safe Logic) ---
chat_db = []
try:
csv_url = "https://huggingface.co/datasets/amkyawdev/AmkyawDev-Dataset/raw/main/train.csv"
df = pd.read_csv(csv_url)
if not df.empty:
for _, row in df.iterrows():
if len(row) >= 2:
u_text = str(row.iloc[0]).strip()
a_text = str(row.iloc[1]).strip()
if u_text.lower() != "nan" and a_text.lower() != "nan":
chat_db.append({"u": u_text, "a": a_text})
print(f"β
Dataset Loaded: {len(chat_db)} pairs.")
except Exception as e:
print(f"β οΈ Dataset Warning: {e}")
# --- αα AI Logic (Llama-3.1-8b-instant) ---
def generate_response(user_input, history):
user_input = user_input.strip()
# Step 1: CSV Matching (Fuzzy Search)
for pair in chat_db:
if SequenceMatcher(None, user_input.lower(), pair["u"].lower()).ratio() > 0.85:
res = pair["a"]
for i in range(len(res)):
time.sleep(0.005)
yield res[: i + 1]
return
# Step 2: Groq API Call
if client:
try:
# Gradio 4 αα²α· [[u, a]] format ααα― Groq αα²α· role format ααΌα±α¬ααΊαΈααΌααΊαΈ
messages = [{"role": "system", "content": "You are Amkyaw AI, a friendly Myanmar assistant. Use Unicode."}]
for h in history:
if h[0]: messages.append({"role": "user", "content": h[0]})
if h[1]: messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": user_input})
stream = client.chat.completions.create(
messages=messages,
model="llama-3.1-8b-instant",
temperature=0.7,
stream=True,
)
full_res = ""
for chunk in stream:
if chunk.choices[0].delta.content:
full_res += chunk.choices[0].delta.content
yield full_res
return
except Exception as e:
print(f"Groq API Error: {e}")
yield "αα±α¬ααΊαΈαααΊαα«αααΊα α‘αα―αα±α¬αα±α¬αααΊ αααΌα±ααα―ααΊαα±αΈαα«αα°αΈαααΊαα»α¬α"
# --- αα UI Design (Gradio 4 Style) ---
custom_css = """
footer { visibility: hidden !important; }
.gradio-container { background: #0d1117 !important; color: white !important; font-family: sans-serif; }
.header-card {
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px; padding: 15px; margin-bottom: 20px;
}
#input-row {
background: #161b22 !important; border-radius: 25px !important;
border: 1px solid #30363d !important; padding: 5px 15px !important;
}
#send-btn {
background: #ffffff !important; color: #000000 !important;
border-radius: 50% !important; width: 40px !important; height: 40px !important; min-width: 40px !important;
}
"""
with gr.Blocks(title="Amkyaw AI v4", css=custom_css) as demo:
gr.HTML("""
<div class="header-card">
<div style="display:flex; justify-content: space-between; align-items: center;">
<b style="color: #58a6ff; font-size: 20px;">Amkyaw AI</b>
<span style="color:#8b949e; font-size:11px;">Groq | Gradio 4 Stable</span>
</div>
</div>
""")
chatbot = gr.Chatbot(show_label=False, height=600)
with gr.Row(elem_id="input-row"):
msg = gr.Textbox(placeholder="Ask me anything...", show_label=False, scale=15, container=False)
submit = gr.Button("β", elem_id="send-btn", scale=1)
# UI Flow logic
def user_flow(user_msg, history):
if not user_msg: return "", history
return "", history + [[user_msg, None]]
def bot_flow(history):
user_input = history[-1][0]
# Streaming response back to chatbot
for chunk in generate_response(user_input, history[:-1]):
history[-1][1] = chunk
yield history
# Event binding
msg.submit(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
submit.click(user_flow, [msg, chatbot], [msg, chatbot], queue=False).then(bot_flow, chatbot, chatbot)
if __name__ == "__main__":
demo.launch() |