Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,6 @@ chat_history: List[Dict[str, str]] = []
|
|
| 16 |
|
| 17 |
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
|
| 18 |
"""Groq API call with chat history and optional summarization"""
|
| 19 |
-
|
| 20 |
if not API_KEY:
|
| 21 |
return "β No API Key found"
|
| 22 |
|
|
@@ -29,7 +28,7 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 29 |
"Content-Type": "application/json"
|
| 30 |
}
|
| 31 |
|
| 32 |
-
# Add current message to history
|
| 33 |
chat_history.append({
|
| 34 |
"role": "user",
|
| 35 |
"content": message.strip(),
|
|
@@ -37,15 +36,14 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 37 |
"timestamp": datetime.now().isoformat()
|
| 38 |
})
|
| 39 |
|
| 40 |
-
# Limit history to max_history messages
|
| 41 |
-
recent_history = chat_history[-max_history:]
|
| 42 |
-
|
| 43 |
# Prepare messages for API call
|
| 44 |
-
messages = [{"role": msg["role"], "content": msg["content"]}
|
|
|
|
| 45 |
|
| 46 |
if summarize:
|
| 47 |
summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
|
| 48 |
-
summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}"
|
|
|
|
| 49 |
messages.append({"role": "user", "content": summary_prompt})
|
| 50 |
|
| 51 |
payload = {
|
|
@@ -100,61 +98,77 @@ def manual_curl_example():
|
|
| 100 |
-d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
|
| 101 |
"""
|
| 102 |
|
| 103 |
-
def
|
| 104 |
-
"""Process message and
|
| 105 |
response = groq_with_memory(message, topic, summarize)
|
| 106 |
-
|
|
|
|
| 107 |
|
| 108 |
-
# Gradio Interface
|
| 109 |
-
with gr.Blocks(title="Groq Chat
|
| 110 |
-
gr.
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
with gr.Column():
|
| 115 |
msg_input = gr.Textbox(
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
topic_input = gr.Textbox(
|
| 120 |
-
label="Topic",
|
| 121 |
value="general"
|
| 122 |
)
|
| 123 |
-
summarize_cb = gr.Checkbox(
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
with gr.Column():
|
| 127 |
-
response_output = gr.Textbox(
|
| 128 |
-
label="Response",
|
| 129 |
-
lines=10,
|
| 130 |
-
interactive=False
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
with gr.Tab("API Tools"):
|
| 149 |
-
gr.Markdown(f"""
|
| 150 |
-
**API Status:** {'β
Ready' if API_KEY else 'β No API Key'}
|
| 151 |
-
**Endpoint:** `{API_URL}`
|
| 152 |
-
**Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
|
| 153 |
-
""")
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
if __name__ == "__main__":
|
| 160 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 16 |
|
| 17 |
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
|
| 18 |
"""Groq API call with chat history and optional summarization"""
|
|
|
|
| 19 |
if not API_KEY:
|
| 20 |
return "β No API Key found"
|
| 21 |
|
|
|
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# Add current message to history
|
| 32 |
chat_history.append({
|
| 33 |
"role": "user",
|
| 34 |
"content": message.strip(),
|
|
|
|
| 36 |
"timestamp": datetime.now().isoformat()
|
| 37 |
})
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
# Prepare messages for API call
|
| 40 |
+
messages = [{"role": msg["role"], "content": msg["content"]}
|
| 41 |
+
for msg in chat_history[-max_history:]]
|
| 42 |
|
| 43 |
if summarize:
|
| 44 |
summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
|
| 45 |
+
summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}"
|
| 46 |
+
for msg in chat_history if msg["topic"] == topic])
|
| 47 |
messages.append({"role": "user", "content": summary_prompt})
|
| 48 |
|
| 49 |
payload = {
|
|
|
|
| 98 |
-d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
|
| 99 |
"""
|
| 100 |
|
| 101 |
+
def process_chat(message: str, chat_history: list, topic: str, summarize: bool):
|
| 102 |
+
"""Process chat message and update history"""
|
| 103 |
response = groq_with_memory(message, topic, summarize)
|
| 104 |
+
chat_history.append((message, response))
|
| 105 |
+
return "", chat_history
|
| 106 |
|
| 107 |
+
# Gradio Interface with Chatbox Style
|
| 108 |
+
with gr.Blocks(title="Groq Chat Interface") as demo:
|
| 109 |
+
with gr.Tabs():
|
| 110 |
+
with gr.TabItem("π¬ Chat"):
|
| 111 |
+
chatbot = gr.Chatbot(height=500)
|
| 112 |
+
with gr.Row():
|
|
|
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
+
placeholder="Type your message here...",
|
| 115 |
+
show_label=False,
|
| 116 |
+
container=False,
|
| 117 |
+
scale=7
|
| 118 |
)
|
| 119 |
+
with gr.Column(scale=1):
|
| 120 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 121 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 122 |
+
|
| 123 |
+
with gr.Accordion("βοΈ Settings", open=False):
|
| 124 |
topic_input = gr.Textbox(
|
| 125 |
+
label="Conversation Topic",
|
| 126 |
value="general"
|
| 127 |
)
|
| 128 |
+
summarize_cb = gr.Checkbox(
|
| 129 |
+
label="Summarize conversation",
|
| 130 |
+
value=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
)
|
| 132 |
+
|
| 133 |
+
# Chat interaction
|
| 134 |
+
msg_input.submit(
|
| 135 |
+
process_chat,
|
| 136 |
+
[msg_input, chatbot, topic_input, summarize_cb],
|
| 137 |
+
[msg_input, chatbot]
|
| 138 |
+
)
|
| 139 |
+
send_btn.click(
|
| 140 |
+
process_chat,
|
| 141 |
+
[msg_input, chatbot, topic_input, summarize_cb],
|
| 142 |
+
[msg_input, chatbot]
|
| 143 |
+
)
|
| 144 |
+
clear_btn.click(lambda: [], None, chatbot)
|
| 145 |
|
| 146 |
+
with gr.TabItem("π History"):
|
| 147 |
+
history_topic = gr.Textbox(label="Filter by Topic")
|
| 148 |
+
view_btn = gr.Button("View History")
|
| 149 |
+
clear_history_btn = gr.Button("Clear All History", variant="stop")
|
| 150 |
+
history_output = gr.Textbox(label="Raw History", lines=15)
|
| 151 |
+
|
| 152 |
+
view_btn.click(
|
| 153 |
+
view_history,
|
| 154 |
+
inputs=[history_topic],
|
| 155 |
+
outputs=[history_output]
|
| 156 |
+
)
|
| 157 |
+
clear_history_btn.click(
|
| 158 |
+
clear_history,
|
| 159 |
+
outputs=[history_output]
|
| 160 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
with gr.TabItem("π§ API Tools"):
|
| 163 |
+
gr.Markdown(f"""
|
| 164 |
+
**API Status:** {'β
Ready' if API_KEY else 'β No API Key'}
|
| 165 |
+
**Endpoint:** `{API_URL}`
|
| 166 |
+
**Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
|
| 167 |
+
""")
|
| 168 |
+
|
| 169 |
+
curl_btn = gr.Button("Generate cURL Example")
|
| 170 |
+
curl_output = gr.Code(label="cURL Command", language="shell")
|
| 171 |
+
curl_btn.click(manual_curl_example, outputs=[curl_output])
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|