Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -74,19 +74,28 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> str:
|
|
74 |
except Exception as e:
|
75 |
return f"{model_name} error: {str(e)}"
|
76 |
|
77 |
-
def respond(message: str, history: List[List[str]], session_id: str) ->
|
78 |
-
"""Handle sequential model responses with
|
79 |
# Load session history
|
80 |
session = session_manager.load_session(session_id)
|
81 |
-
messages = [{"role": "user", "content": message}]
|
82 |
|
83 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
session["history"].append({
|
85 |
"timestamp": datetime.now().isoformat(),
|
86 |
"type": "user",
|
87 |
"content": message
|
88 |
})
|
89 |
-
|
|
|
90 |
|
91 |
# Get first model's response
|
92 |
response1 = query_model("Qwen2.5-Coder-32B-Instruct", messages)
|
@@ -96,14 +105,8 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
|
|
96 |
"model": "Qwen2.5-Coder-32B-Instruct",
|
97 |
"content": response1
|
98 |
})
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
# Add first response to context
|
103 |
-
messages.append({
|
104 |
-
"role": "assistant",
|
105 |
-
"content": f"Previous response: {response1}"
|
106 |
-
})
|
107 |
|
108 |
# Get second model's response
|
109 |
response2 = query_model("Qwen2.5-72B-Instruct", messages)
|
@@ -113,14 +116,8 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
|
|
113 |
"model": "Qwen2.5-72B-Instruct",
|
114 |
"content": response2
|
115 |
})
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
# Add second response to context
|
120 |
-
messages.append({
|
121 |
-
"role": "assistant",
|
122 |
-
"content": f"Previous responses: {response1}\n{response2}"
|
123 |
-
})
|
124 |
|
125 |
# Get final model's response
|
126 |
response3 = query_model("Llama3.3-70B-Instruct", messages)
|
@@ -130,8 +127,14 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
|
|
130 |
"model": "Llama3.3-70B-Instruct",
|
131 |
"content": response3
|
132 |
})
|
|
|
|
|
|
|
|
|
133 |
session_manager.save_session(session_id, session)
|
134 |
-
|
|
|
|
|
135 |
|
136 |
# Create the Gradio interface with session management
|
137 |
with gr.Blocks(title="Multi-LLM Collaboration Chat") as demo:
|
|
|
74 |
except Exception as e:
|
75 |
return f"{model_name} error: {str(e)}"
|
76 |
|
77 |
+
def respond(message: str, history: List[List[str]], session_id: str) -> List[List[str]]:
|
78 |
+
"""Handle sequential model responses with context preservation"""
|
79 |
# Load session history
|
80 |
session = session_manager.load_session(session_id)
|
|
|
81 |
|
82 |
+
# Build context from session history
|
83 |
+
messages = []
|
84 |
+
for entry in session["history"]:
|
85 |
+
if entry["type"] == "user":
|
86 |
+
messages.append({"role": "user", "content": entry["content"]})
|
87 |
+
else:
|
88 |
+
messages.append({"role": "assistant", "content": f"{entry['model']}: {entry['content']}"})
|
89 |
+
|
90 |
+
# Add current message
|
91 |
+
messages.append({"role": "user", "content": message})
|
92 |
session["history"].append({
|
93 |
"timestamp": datetime.now().isoformat(),
|
94 |
"type": "user",
|
95 |
"content": message
|
96 |
})
|
97 |
+
|
98 |
+
responses = []
|
99 |
|
100 |
# Get first model's response
|
101 |
response1 = query_model("Qwen2.5-Coder-32B-Instruct", messages)
|
|
|
105 |
"model": "Qwen2.5-Coder-32B-Instruct",
|
106 |
"content": response1
|
107 |
})
|
108 |
+
messages.append({"role": "assistant", "content": f"Qwen2.5-Coder-32B-Instruct: {response1}"})
|
109 |
+
responses.append(f"**Qwen2.5-Coder-32B-Instruct**:\n{response1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# Get second model's response
|
112 |
response2 = query_model("Qwen2.5-72B-Instruct", messages)
|
|
|
116 |
"model": "Qwen2.5-72B-Instruct",
|
117 |
"content": response2
|
118 |
})
|
119 |
+
messages.append({"role": "assistant", "content": f"Qwen2.5-72B-Instruct: {response2}"})
|
120 |
+
responses.append(f"**Qwen2.5-72B-Instruct**:\n{response2}")
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# Get final model's response
|
123 |
response3 = query_model("Llama3.3-70B-Instruct", messages)
|
|
|
127 |
"model": "Llama3.3-70B-Instruct",
|
128 |
"content": response3
|
129 |
})
|
130 |
+
messages.append({"role": "assistant", "content": f"Llama3.3-70B-Instruct: {response3}"})
|
131 |
+
responses.append(f"**Llama3.3-70B-Instruct**:\n{response3}")
|
132 |
+
|
133 |
+
# Save final session state
|
134 |
session_manager.save_session(session_id, session)
|
135 |
+
|
136 |
+
# Return responses in Gradio chat format
|
137 |
+
return [[message, response] for response in responses]
|
138 |
|
139 |
# Create the Gradio interface with session management
|
140 |
with gr.Blocks(title="Multi-LLM Collaboration Chat") as demo:
|