Spaces:
Sleeping
Sleeping
File size: 5,656 Bytes
a312592 6f050d2 2f22113 550f98e ac1d1c0 6f050d2 b64c343 a312592 b64c343 a312592 b64c343 ac1d1c0 a312592 550f98e a312592 862323b 2f22113 a312592 ac1d1c0 a312592 ac1d1c0 a312592 ac1d1c0 a312592 b64c343 a312592 c5fe00a a312592 c5fe00a a312592 b64c343 550f98e a312592 2f22113 a312592 7103644 a312592 ac1d1c0 a312592 ac1d1c0 a312592 ac1d1c0 a312592 1c6ad60 3a01f98 a312592 c5fe00a a312592 1c6ad60 a312592 1c6ad60 a312592 3a01f98 a312592 7108e4b 4a5989e a312592 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import gradio as gr
import requests
import os
import json
from typing import List, Dict
from datetime import datetime
# Groq API Configuration
API_URL = "https://api.groq.com/openai/v1/chat/completions"
API_KEY = os.getenv("GROQ_API_KEY")
print(f"π API Key Found: {'Yes' if API_KEY else 'No'}")
# In-memory chat history storage
chat_history: List[Dict[str, str]] = []
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
"""Groq API call with chat history and optional summarization"""
if not API_KEY:
return "β No API Key found"
if not message.strip():
return "β Empty message"
try:
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
# Add current message to history with timestamp and topic
chat_history.append({
"role": "user",
"content": message.strip(),
"topic": topic,
"timestamp": datetime.now().isoformat()
})
# Limit history to max_history messages
recent_history = chat_history[-max_history:]
# Prepare messages for API call
messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
if summarize:
summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}" for msg in recent_history])
messages.append({"role": "user", "content": summary_prompt})
payload = {
"model": "llama-3.3-70b-versatile",
"messages": messages,
"max_tokens": 5000,
"temperature": 0.7
}
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
if response.status_code == 200:
result = response.json()
if "choices" in result and result["choices"]:
response_content = result["choices"][0]["message"]["content"]
chat_history.append({
"role": "assistant",
"content": response_content,
"topic": topic,
"timestamp": datetime.now().isoformat()
})
return response_content
return f"β No choices in response: {result}"
return f"β HTTP {response.status_code}: {response.text}"
except Exception as e:
return f"β Error: {str(e)}"
def clear_history():
"""Clear the chat history"""
chat_history.clear()
return "β
Chat history cleared"
def view_history(topic: str = None):
"""View chat history with optional topic filter"""
if not chat_history:
return "β No chat history available"
filtered = [msg for msg in chat_history if topic is None or msg["topic"] == topic]
return "\n".join(
f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
for msg in filtered
)
def manual_curl_example():
"""Generate example curl command"""
if not API_KEY:
return "β No API key available"
return f"""curl -X POST "{API_URL}" \\
-H "Authorization: Bearer {API_KEY}" \\
-H "Content-Type: application/json" \\
-d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
"""
def process_message(message: str, topic: str, summarize: bool):
"""Process message and clear input"""
response = groq_with_memory(message, topic, summarize)
return response, ""
# Gradio Interface
with gr.Blocks(title="Groq Chat Debugger") as demo:
gr.Markdown("## π¬ Groq API Chat Interface")
with gr.Tab("Chat"):
with gr.Row():
with gr.Column():
msg_input = gr.Textbox(
label="Your Message",
value="",
placeholder="Tulis di sini..."
)
topic_input = gr.Textbox(
label="Topic",
value="general"
)
summarize_cb = gr.Checkbox(label="Summarize conversation")
send_btn = gr.Button("Send", variant="primary")
with gr.Column():
response_output = gr.Textbox(
label="Response",
lines=10,
interactive=False
)
send_btn.click(
process_message,
inputs=[msg_input, topic_input, summarize_cb],
outputs=[response_output, msg_input]
)
with gr.Tab("History"):
history_topic = gr.Textbox(label="Filter by Topic")
view_btn = gr.Button("View History")
clear_btn = gr.Button("Clear History", variant="stop")
history_output = gr.Textbox(label="Chat History", lines=10)
view_btn.click(view_history, inputs=[history_topic], outputs=[history_output])
clear_btn.click(clear_history, outputs=[history_output])
with gr.Tab("API Tools"):
gr.Markdown(f"""
**API Status:** {'β
Ready' if API_KEY else 'β No API Key'}
**Endpoint:** `{API_URL}`
**Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
""")
curl_btn = gr.Button("Generate cURL Example")
curl_output = gr.Code(label="cURL Command", language="shell")
curl_btn.click(manual_curl_example, outputs=[curl_output])
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) |