Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ MODELS = [
|
|
12 |
("Llama 3.2 Vision", "meta-llama/llama-3.2-11b-vision-instruct:free")
|
13 |
]
|
14 |
|
15 |
-
def ask_ai(message,
|
16 |
"""Basic AI query function"""
|
17 |
# Get model ID
|
18 |
model_id = MODELS[0][1] # Default
|
@@ -21,9 +21,9 @@ def ask_ai(message, history, model_choice):
|
|
21 |
model_id = model_id_value
|
22 |
break
|
23 |
|
24 |
-
# Create messages from history
|
25 |
messages = []
|
26 |
-
for human_msg, ai_msg in
|
27 |
messages.append({"role": "user", "content": human_msg})
|
28 |
if ai_msg:
|
29 |
messages.append({"role": "assistant", "content": ai_msg})
|
@@ -52,30 +52,61 @@ def ask_ai(message, history, model_choice):
|
|
52 |
if response.status_code == 200:
|
53 |
result = response.json()
|
54 |
ai_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
55 |
-
|
56 |
else:
|
57 |
-
|
58 |
except Exception as e:
|
59 |
-
|
60 |
|
61 |
-
return
|
|
|
|
|
|
|
62 |
|
63 |
# Create minimal interface
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
gr.
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
# Launch directly with Gradio's built-in server
|
81 |
if __name__ == "__main__":
|
|
|
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
|
|
|
21 |
model_id = model_id_value
|
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})
|
|
|
52 |
if response.status_code == 200:
|
53 |
result = response.json()
|
54 |
ai_response = result.get("choices", [{}])[0].get("message", {}).get("content", "")
|
55 |
+
chatbot.append((message, ai_response))
|
56 |
else:
|
57 |
+
chatbot.append((message, f"Error: Status code {response.status_code}"))
|
58 |
except Exception as e:
|
59 |
+
chatbot.append((message, f"Error: {str(e)}"))
|
60 |
|
61 |
+
return chatbot, ""
|
62 |
+
|
63 |
+
def clear_chat():
|
64 |
+
return [], ""
|
65 |
|
66 |
# Create minimal interface
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.Markdown("# Simple AI Chat")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
+
with gr.Column():
|
72 |
+
chatbot = gr.Chatbot(height=400, type="messages")
|
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(
|
94 |
+
fn=ask_ai,
|
95 |
+
inputs=[message, chatbot, model_choice],
|
96 |
+
outputs=[chatbot, message]
|
97 |
+
)
|
98 |
+
|
99 |
+
message.submit(
|
100 |
+
fn=ask_ai,
|
101 |
+
inputs=[message, chatbot, model_choice],
|
102 |
+
outputs=[chatbot, message]
|
103 |
+
)
|
104 |
+
|
105 |
+
clear_btn.click(
|
106 |
+
fn=clear_chat,
|
107 |
+
inputs=[],
|
108 |
+
outputs=[chatbot, message]
|
109 |
+
)
|
110 |
|
111 |
# Launch directly with Gradio's built-in server
|
112 |
if __name__ == "__main__":
|