Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,193 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
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 |
-
gr.
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
)
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
import google.generativeai as genai
|
7 |
+
from datetime import datetime
|
8 |
|
9 |
+
# Configure API keys
|
10 |
+
openai_api_key = "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"
|
11 |
+
together_api_key = "5183aeea800941cd193e64df325814cb9253f4a4083917335e4d4b75a12e87fd"
|
12 |
+
gemini_api_key = "AIzaSyDvZ2-im5lt0-Tundiq562lyi_cmPJxD4g"
|
13 |
|
14 |
+
# Configure API clients
|
15 |
+
openai.api_key = openai_api_key
|
16 |
+
genai.configure(api_key=gemini_api_key)
|
17 |
|
18 |
+
# Initialize conversation history
|
19 |
+
conversation_history = []
|
20 |
+
learning_data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# Function to generate response using OpenAI
|
23 |
+
def generate_openai_response(message, model="gpt-3.5-turbo"):
|
24 |
+
conversation_history.append({"role": "user", "content": message})
|
25 |
+
|
26 |
+
try:
|
27 |
+
response = openai.ChatCompletion.create(
|
28 |
+
model=model,
|
29 |
+
messages=conversation_history
|
30 |
+
)
|
31 |
+
|
32 |
+
assistant_message = response.choices[0].message.content
|
33 |
+
conversation_history.append({"role": "assistant", "content": assistant_message})
|
34 |
+
|
35 |
+
# Save for learning
|
36 |
+
save_for_learning(message, assistant_message, "openai", model)
|
37 |
+
|
38 |
+
return assistant_message
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error with OpenAI: {str(e)}"
|
41 |
|
42 |
+
# Function to generate response using Together AI
|
43 |
+
def generate_together_response(message, model="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8"):
|
44 |
+
conversation_history.append({"role": "user", "content": message})
|
45 |
+
|
46 |
+
try:
|
47 |
+
headers = {
|
48 |
+
"Authorization": f"Bearer {together_api_key}",
|
49 |
+
"Content-Type": "application/json"
|
50 |
+
}
|
51 |
+
|
52 |
+
data = {
|
53 |
+
"model": model,
|
54 |
+
"messages": [{"role": "user", "content": message}]
|
55 |
+
}
|
56 |
+
|
57 |
+
response = requests.post(
|
58 |
+
"https://api.together.xyz/v1/chat/completions",
|
59 |
+
headers=headers,
|
60 |
+
json=data
|
61 |
+
)
|
62 |
+
|
63 |
+
if response.status_code == 200:
|
64 |
+
result = response.json()
|
65 |
+
assistant_message = result["choices"][0]["message"]["content"]
|
66 |
+
conversation_history.append({"role": "assistant", "content": assistant_message})
|
67 |
+
|
68 |
+
# Save for learning
|
69 |
+
save_for_learning(message, assistant_message, "together", model)
|
70 |
+
|
71 |
+
return assistant_message
|
72 |
+
else:
|
73 |
+
return f"Error with Together AI: {response.text}"
|
74 |
+
except Exception as e:
|
75 |
+
return f"Error with Together AI: {str(e)}"
|
76 |
|
77 |
+
# Function to generate response using Google Gemini
|
78 |
+
def generate_gemini_response(message, model="gemini-1.0-pro"):
|
79 |
+
conversation_history.append({"role": "user", "content": message})
|
80 |
+
|
81 |
+
try:
|
82 |
+
gemini_model = genai.GenerativeModel(model)
|
83 |
+
response = gemini_model.generate_content(message)
|
84 |
+
|
85 |
+
assistant_message = response.text
|
86 |
+
conversation_history.append({"role": "assistant", "content": assistant_message})
|
87 |
+
|
88 |
+
# Save for learning
|
89 |
+
save_for_learning(message, assistant_message, "gemini", model)
|
90 |
+
|
91 |
+
return assistant_message
|
92 |
+
except Exception as e:
|
93 |
+
return f"Error with Google Gemini: {str(e)}"
|
94 |
|
95 |
+
# Function to save data for learning
|
96 |
+
def save_for_learning(user_message, assistant_message, provider, model):
|
97 |
+
timestamp = datetime.now().isoformat()
|
98 |
+
|
99 |
+
entry = {
|
100 |
+
"timestamp": timestamp,
|
101 |
+
"user_message": user_message,
|
102 |
+
"assistant_message": assistant_message,
|
103 |
+
"provider": provider,
|
104 |
+
"model": model
|
105 |
+
}
|
106 |
+
|
107 |
+
# In a real system, this would be saved to a database
|
108 |
+
# For this demo, we'll just keep it in memory
|
109 |
+
if "conversations" not in learning_data:
|
110 |
+
learning_data["conversations"] = []
|
111 |
+
|
112 |
+
learning_data["conversations"].append(entry)
|
113 |
+
|
114 |
+
# Trigger autopilot learning (in a real system, this would be a background process)
|
115 |
+
autopilot_learning()
|
116 |
|
117 |
+
# Function for autopilot learning
|
118 |
+
def autopilot_learning():
|
119 |
+
# In a real system, this would:
|
120 |
+
# 1. Analyze past conversations to identify knowledge gaps
|
121 |
+
# 2. Research topics to fill those gaps
|
122 |
+
# 3. Update the model's knowledge base
|
123 |
+
# 4. Improve response quality over time
|
124 |
+
|
125 |
+
# For this demo, we'll just log that learning occurred
|
126 |
+
timestamp = datetime.now().isoformat()
|
127 |
+
|
128 |
+
if "autopilot_events" not in learning_data:
|
129 |
+
learning_data["autopilot_events"] = []
|
130 |
+
|
131 |
+
learning_data["autopilot_events"].append({
|
132 |
+
"timestamp": timestamp,
|
133 |
+
"event": "Autopilot learning cycle completed",
|
134 |
+
"conversations_analyzed": len(learning_data.get("conversations", []))
|
135 |
+
})
|
136 |
|
137 |
+
# Function to handle chat based on selected model
|
138 |
+
def chat(message, model_choice):
|
139 |
+
if not message:
|
140 |
+
return "Please enter a message."
|
141 |
+
|
142 |
+
if model_choice == "OpenAI GPT-3.5":
|
143 |
+
return generate_openai_response(message, "gpt-3.5-turbo")
|
144 |
+
elif model_choice == "OpenAI GPT-4":
|
145 |
+
return generate_openai_response(message, "gpt-4")
|
146 |
+
elif model_choice == "Together AI Llama":
|
147 |
+
return generate_together_response(message, "meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8")
|
148 |
+
elif model_choice == "Together AI Mistral":
|
149 |
+
return generate_together_response(message, "mistralai/Mistral-7B-Instruct-v0.1")
|
150 |
+
elif model_choice == "Google Gemini Pro":
|
151 |
+
return generate_gemini_response(message, "gemini-1.0-pro")
|
152 |
+
elif model_choice == "Google Gemini Flash":
|
153 |
+
return generate_gemini_response(message, "gemini-2.0-flash")
|
154 |
+
else:
|
155 |
+
return "Please select a valid model."
|
156 |
|
157 |
+
# Create Gradio interface
|
158 |
+
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
159 |
+
gr.Markdown("# ML Agent System with Autopilot Learning")
|
160 |
+
gr.Markdown("This system supports multiple AI models and features continuous learning in autopilot mode.")
|
161 |
+
|
162 |
+
with gr.Row():
|
163 |
+
with gr.Column(scale=4):
|
164 |
+
chatbot = gr.Chatbot(height=400)
|
165 |
+
msg = gr.Textbox(label="Type your message here", placeholder="Ask me anything...")
|
166 |
+
clear = gr.Button("Clear Conversation")
|
167 |
+
|
168 |
+
with gr.Column(scale=1):
|
169 |
+
model = gr.Radio(
|
170 |
+
["OpenAI GPT-3.5", "OpenAI GPT-4", "Together AI Llama", "Together AI Mistral", "Google Gemini Pro", "Google Gemini Flash"],
|
171 |
+
label="Select AI Model",
|
172 |
+
value="OpenAI GPT-3.5"
|
173 |
+
)
|
174 |
+
gr.Markdown("### System Features")
|
175 |
+
gr.Markdown("- Multi-model support")
|
176 |
+
gr.Markdown("- Continuous learning")
|
177 |
+
gr.Markdown("- Autopilot research mode")
|
178 |
+
gr.Markdown("- Knowledge retention")
|
179 |
+
|
180 |
+
def respond(message, chat_history, model_choice):
|
181 |
+
if not message:
|
182 |
+
return "", chat_history
|
183 |
+
|
184 |
+
bot_message = chat(message, model_choice)
|
185 |
+
chat_history.append((message, bot_message))
|
186 |
+
return "", chat_history
|
187 |
+
|
188 |
+
msg.submit(respond, [msg, chatbot, model], [msg, chatbot])
|
189 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
190 |
|
191 |
+
# Launch the app
|
192 |
if __name__ == "__main__":
|
193 |
+
demo.launch(share=True)
|