Spaces:
Sleeping
Sleeping
v5.0: Minimal version
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ from difflib import SequenceMatcher
|
|
| 7 |
|
| 8 |
# Get API Key
|
| 9 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 10 |
-
print(f"π API Key: {bool(GROQ_API_KEY)}")
|
| 11 |
|
| 12 |
client = None
|
| 13 |
if GROQ_API_KEY:
|
|
@@ -33,24 +33,27 @@ try:
|
|
| 33 |
if user_text != "nan" and assistant_text != "nan" and user_text and assistant_text:
|
| 34 |
chat_db.append({"u": user_text, "a": assistant_text})
|
| 35 |
|
| 36 |
-
print(f"β
Dataset: {len(chat_db)} pairs
|
| 37 |
except Exception as e:
|
| 38 |
print(f"β οΈ Dataset error: {e}")
|
| 39 |
|
| 40 |
# --- AI Logic ---
|
| 41 |
def generate_response(user_input):
|
| 42 |
-
user_input = user_input.strip()
|
| 43 |
|
| 44 |
# Priority 1: Dataset Match
|
| 45 |
for pair in chat_db:
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
res = pair["a"]
|
| 48 |
for i in range(len(res)):
|
| 49 |
time.sleep(0.003)
|
| 50 |
yield res[: i + 1]
|
| 51 |
return
|
| 52 |
|
| 53 |
-
if SequenceMatcher(None,
|
| 54 |
res = pair["a"]
|
| 55 |
for i in range(len(res)):
|
| 56 |
time.sleep(0.003)
|
|
@@ -63,13 +66,12 @@ def generate_response(user_input):
|
|
| 63 |
return
|
| 64 |
|
| 65 |
try:
|
| 66 |
-
# Simple messages without history
|
| 67 |
messages = [
|
| 68 |
-
{"role": "system", "content": "You are
|
| 69 |
{"role": "user", "content": user_input}
|
| 70 |
]
|
| 71 |
|
| 72 |
-
print(f"π¨
|
| 73 |
|
| 74 |
response = client.chat.completions.create(
|
| 75 |
model="llama-3.1-8b-instant",
|
|
@@ -79,38 +81,14 @@ def generate_response(user_input):
|
|
| 79 |
)
|
| 80 |
yield response.choices[0].message.content
|
| 81 |
except Exception as e:
|
| 82 |
-
print(f"β
|
| 83 |
yield f"Error: {str(e)}"
|
| 84 |
|
| 85 |
# --- UI ---
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
.gradio-container { background: #0d1117 !important; color: white !important; }
|
| 89 |
-
.header-card {
|
| 90 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 91 |
-
border-radius: 20px; padding: 20px;
|
| 92 |
-
}
|
| 93 |
-
#input-row {
|
| 94 |
-
background: rgba(255,255,255,0.1);
|
| 95 |
-
border: 2px solid #667eea;
|
| 96 |
-
border-radius: 30px;
|
| 97 |
-
padding: 8px;
|
| 98 |
-
}
|
| 99 |
-
#send-btn {
|
| 100 |
-
background: linear-gradient(90deg, #667eea, #764ba2) !important;
|
| 101 |
-
border-radius: 50%;
|
| 102 |
-
}
|
| 103 |
-
"""
|
| 104 |
-
|
| 105 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 106 |
-
gr.HTML("""
|
| 107 |
-
<div class="header-card">
|
| 108 |
-
<b style="color:white;font-size:24px;">π€ Amkyaw AI</b>
|
| 109 |
-
<span style="color:white;font-size:12px;">v4.9</span>
|
| 110 |
-
</div>
|
| 111 |
-
""")
|
| 112 |
chatbot = gr.Chatbot(height=600)
|
| 113 |
-
with gr.Row(
|
| 114 |
msg = gr.Textbox(placeholder="αα±αΈαα»ααΊαα¬αα±αΈαα«...", container=False, scale=15)
|
| 115 |
btn = gr.Button("β", scale=1)
|
| 116 |
|
|
@@ -119,10 +97,8 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 119 |
return "", hist + [[msg, None]]
|
| 120 |
|
| 121 |
def respond(hist):
|
| 122 |
-
if not hist or not hist[-1]:
|
| 123 |
-
|
| 124 |
-
text = hist[-1][0]
|
| 125 |
-
for reply in generate_response(text):
|
| 126 |
hist[-1][1] = reply
|
| 127 |
yield hist
|
| 128 |
|
|
|
|
| 7 |
|
| 8 |
# Get API Key
|
| 9 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
| 10 |
+
print(f"π API Key exists: {bool(GROQ_API_KEY)}")
|
| 11 |
|
| 12 |
client = None
|
| 13 |
if GROQ_API_KEY:
|
|
|
|
| 33 |
if user_text != "nan" and assistant_text != "nan" and user_text and assistant_text:
|
| 34 |
chat_db.append({"u": user_text, "a": assistant_text})
|
| 35 |
|
| 36 |
+
print(f"β
Dataset: {len(chat_db)} pairs")
|
| 37 |
except Exception as e:
|
| 38 |
print(f"β οΈ Dataset error: {e}")
|
| 39 |
|
| 40 |
# --- AI Logic ---
|
| 41 |
def generate_response(user_input):
|
| 42 |
+
user_input = str(user_input).strip()
|
| 43 |
|
| 44 |
# Priority 1: Dataset Match
|
| 45 |
for pair in chat_db:
|
| 46 |
+
user_lower = pair["u"].lower().strip()
|
| 47 |
+
input_lower = user_input.lower()
|
| 48 |
+
|
| 49 |
+
if user_lower == input_lower:
|
| 50 |
res = pair["a"]
|
| 51 |
for i in range(len(res)):
|
| 52 |
time.sleep(0.003)
|
| 53 |
yield res[: i + 1]
|
| 54 |
return
|
| 55 |
|
| 56 |
+
if SequenceMatcher(None, input_lower, user_lower).ratio() > 0.80:
|
| 57 |
res = pair["a"]
|
| 58 |
for i in range(len(res)):
|
| 59 |
time.sleep(0.003)
|
|
|
|
| 66 |
return
|
| 67 |
|
| 68 |
try:
|
|
|
|
| 69 |
messages = [
|
| 70 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 71 |
{"role": "user", "content": user_input}
|
| 72 |
]
|
| 73 |
|
| 74 |
+
print(f"π¨ Messages: {messages}")
|
| 75 |
|
| 76 |
response = client.chat.completions.create(
|
| 77 |
model="llama-3.1-8b-instant",
|
|
|
|
| 81 |
)
|
| 82 |
yield response.choices[0].message.content
|
| 83 |
except Exception as e:
|
| 84 |
+
print(f"β Error: {e}")
|
| 85 |
yield f"Error: {str(e)}"
|
| 86 |
|
| 87 |
# --- UI ---
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.HTML("<h2>π€ Amkyaw AI v5.0</h2>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
chatbot = gr.Chatbot(height=600)
|
| 91 |
+
with gr.Row():
|
| 92 |
msg = gr.Textbox(placeholder="αα±αΈαα»ααΊαα¬αα±αΈαα«...", container=False, scale=15)
|
| 93 |
btn = gr.Button("β", scale=1)
|
| 94 |
|
|
|
|
| 97 |
return "", hist + [[msg, None]]
|
| 98 |
|
| 99 |
def respond(hist):
|
| 100 |
+
if not hist or not hist[-1]: return hist
|
| 101 |
+
for reply in generate_response(hist[-1][0]):
|
|
|
|
|
|
|
| 102 |
hist[-1][1] = reply
|
| 103 |
yield hist
|
| 104 |
|