Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,23 @@ MODELS = [
|
|
| 12 |
("Llama 3.2 Vision", "meta-llama/llama-3.2-11b-vision-instruct:free")
|
| 13 |
]
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def ask_ai(message, chatbot, model_choice):
|
| 16 |
"""Basic AI query function"""
|
|
|
|
|
|
|
|
|
|
| 17 |
# Get model ID
|
| 18 |
model_id = MODELS[0][1] # Default
|
| 19 |
for name, model_id_value in MODELS:
|
|
@@ -22,11 +37,7 @@ def ask_ai(message, chatbot, model_choice):
|
|
| 22 |
break
|
| 23 |
|
| 24 |
# Create messages from chatbot history
|
| 25 |
-
messages =
|
| 26 |
-
for human_msg, ai_msg in chatbot:
|
| 27 |
-
messages.append({"role": "user", "content": human_msg})
|
| 28 |
-
if ai_msg:
|
| 29 |
-
messages.append({"role": "assistant", "content": ai_msg})
|
| 30 |
|
| 31 |
# Add current message
|
| 32 |
messages.append({"role": "user", "content": message})
|
|
@@ -52,11 +63,11 @@ def ask_ai(message, chatbot, model_choice):
|
|
| 52 |
if response.status_code == 200:
|
| 53 |
result = response.json()
|
| 54 |
ai_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 55 |
-
chatbot
|
| 56 |
else:
|
| 57 |
-
chatbot
|
| 58 |
except Exception as e:
|
| 59 |
-
chatbot
|
| 60 |
|
| 61 |
return chatbot, ""
|
| 62 |
|
|
@@ -67,27 +78,25 @@ def clear_chat():
|
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("# Simple AI Chat")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
with gr.Row():
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
with gr.Row():
|
| 75 |
-
message = gr.Textbox(
|
| 76 |
-
placeholder="Type your message here...",
|
| 77 |
-
label="Message",
|
| 78 |
-
lines=3
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
with gr.Row():
|
| 82 |
-
model_choice = gr.Radio(
|
| 83 |
-
[name for name, _ in MODELS],
|
| 84 |
-
value=MODELS[0][0],
|
| 85 |
-
label="Model"
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
with gr.Row():
|
| 89 |
-
submit_btn = gr.Button("Send")
|
| 90 |
-
clear_btn = gr.Button("Clear Chat")
|
| 91 |
|
| 92 |
# Set up events
|
| 93 |
submit_btn.click(
|
|
|
|
| 12 |
("Llama 3.2 Vision", "meta-llama/llama-3.2-11b-vision-instruct:free")
|
| 13 |
]
|
| 14 |
|
| 15 |
+
def format_to_message_dict(history):
|
| 16 |
+
"""Convert history to proper message format"""
|
| 17 |
+
messages = []
|
| 18 |
+
for pair in history:
|
| 19 |
+
if len(pair) == 2:
|
| 20 |
+
human, ai = pair
|
| 21 |
+
if human:
|
| 22 |
+
messages.append({"role": "user", "content": human})
|
| 23 |
+
if ai:
|
| 24 |
+
messages.append({"role": "assistant", "content": ai})
|
| 25 |
+
return messages
|
| 26 |
+
|
| 27 |
def ask_ai(message, chatbot, model_choice):
|
| 28 |
"""Basic AI query function"""
|
| 29 |
+
if not message.strip():
|
| 30 |
+
return chatbot, ""
|
| 31 |
+
|
| 32 |
# Get model ID
|
| 33 |
model_id = MODELS[0][1] # Default
|
| 34 |
for name, model_id_value in MODELS:
|
|
|
|
| 37 |
break
|
| 38 |
|
| 39 |
# Create messages from chatbot history
|
| 40 |
+
messages = format_to_message_dict(chatbot)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Add current message
|
| 43 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 63 |
if response.status_code == 200:
|
| 64 |
result = response.json()
|
| 65 |
ai_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
| 66 |
+
chatbot = chatbot + [[message, ai_response]]
|
| 67 |
else:
|
| 68 |
+
chatbot = chatbot + [[message, f"Error: Status code {response.status_code}"]]
|
| 69 |
except Exception as e:
|
| 70 |
+
chatbot = chatbot + [[message, f"Error: {str(e)}"]]
|
| 71 |
|
| 72 |
return chatbot, ""
|
| 73 |
|
|
|
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
gr.Markdown("# Simple AI Chat")
|
| 80 |
|
| 81 |
+
chatbot = gr.Chatbot(height=400)
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
message = gr.Textbox(
|
| 85 |
+
placeholder="Type your message here...",
|
| 86 |
+
label="Message",
|
| 87 |
+
lines=2
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
model_choice = gr.Radio(
|
| 92 |
+
[name for name, _ in MODELS],
|
| 93 |
+
value=MODELS[0][0],
|
| 94 |
+
label="Model"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
with gr.Row():
|
| 98 |
+
submit_btn = gr.Button("Send")
|
| 99 |
+
clear_btn = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Set up events
|
| 102 |
submit_btn.click(
|