File size: 6,349 Bytes
6f050d2
2f22113
 
550f98e
ac1d1c0
 
6f050d2
b64c343
 
a312592
c8ce7d7
 
b64c343
ac1d1c0
 
 
919808b
a312592
 
c8ce7d7
 
 
550f98e
c8ce7d7
862323b
2f22113
a312592
 
 
 
 
c8ce7d7
ac1d1c0
 
a312592
ac1d1c0
 
 
 
c8ce7d7
a312592
c8ce7d7
 
a312592
ac1d1c0
 
a312592
 
 
b64c343
a312592
0861547
2232670
919808b
 
a312592
 
 
b64c343
919808b
a312592
 
 
 
 
 
 
 
 
 
c8ce7d7
 
 
2f22113
c8ce7d7
7103644
f213ab0
 
919808b
a312592
f213ab0
ac1d1c0
a312592
c8ce7d7
ac1d1c0
c8ce7d7
a312592
c8ce7d7
a312592
f213ab0
a312592
 
ac1d1c0
3a01f98
f949378
597099d
919808b
 
 
597099d
 
919808b
 
597099d
 
919808b
597099d
 
f98b0a4
c8ce7d7
 
f213ab0
 
 
 
 
 
 
 
 
 
 
0280080
c351b7a
 
 
 
 
 
 
 
919808b
c351b7a
 
 
 
 
 
 
 
 
 
 
 
 
 
919808b
 
 
 
 
 
 
c351b7a
f949378
c351b7a
919808b
c351b7a
919808b
0280080
c8ce7d7
f213ab0
919808b
f213ab0
919808b
c8ce7d7
 
 
f213ab0
 
c8ce7d7
919808b
 
 
 
 
 
1e81a19
f213ab0
 
 
 
 
 
 
919808b
7108e4b
4a5989e
c8ce7d7
 
 
f213ab0
919808b
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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": "gemma2-9b-it",
            "messages": messages,
            "max_tokens":2000,
            "temperature":0.9
        }
        
        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"""
    global 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.get("topic") == topic]
    return "\n".join(
        f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
        for msg in filtered
    )

# Gradio Interface
with gr.Blocks(title="Groq Chat Interface", theme=gr.themes.Soft(), css="""
    .gradio-container {
        max-width:100% !important;
        padding-left:0px !important;
        padding-right:0px !important;
    }
    .main {
        max-width:100% !important;
        padding:0px !important;
    }
    .block {
        max-width:100% !important;
    }
""") as demo:
    gr.Markdown("## πŸ’¬ Groq API Chat Interface")
    
    with gr.Tab("πŸ’¬ Chat"):
        # Response area at top
        response_output = gr.Textbox(
            label="πŸ€– AI Response", 
            lines=15,
            interactive=False,
            placeholder="AI responses will appear here...",
            show_copy_button=True
        )
        
        gr.Markdown("---")
        
        # Input area at bottom
        with gr.Row():
            with gr.Column(scale=4):
                msg_input = gr.Textbox(
                    label="✍️ Your Message", 
                    placeholder="Type your message here...",
                    lines=3,
                    max_lines=10
                )
                    
            with gr.Column(scale=1):
                topic_input = gr.Textbox(
                    label="🏷️ Topic", 
                    value="general",
                    placeholder="conversation topic"
                )
                summarize_cb = gr.Checkbox(
                    label="πŸ“‹ Summarize conversation",
                    value=False
                )
                send_btn = gr.Button("πŸ“€ Send Message", variant="primary")
        
        with gr.Row():
            clear_btn = gr.Button("πŸ—‘οΈ Clear Response", variant="secondary")
            history_btn = gr.Button("πŸ“š View History", variant="secondary")
            history_output = gr.Textbox(
                label="πŸ“š Chat History", 
                lines=15,
                interactive=False,
                placeholder="Chat history will appear here..."
            )
        
        # Event handlers - Fixed indentation
        send_btn.click(
            lambda message, topic, summarize: groq_with_memory(message, topic, summarize),
            inputs=[msg_input, topic_input, summarize_cb],
            outputs=[response_output]
        )
        
        msg_input.submit(
            lambda message, topic, summarize: groq_with_memory(message, topic, summarize),
            inputs=[msg_input, topic_input, summarize_cb],
            outputs=[response_output]
        )
        
        clear_btn.click(
            lambda: ("", ""),
            outputs=[response_output, msg_input]
        )
        
        history_btn.click(
            lambda topic: view_history(topic),
            inputs=[topic_input],
            outputs=[history_output]
        )
  

# Launch message
print("πŸš€ Starting Groq Chat Interface...")
print(f"🌐 Access at: http://localhost:7860")
if API_KEY:
    print("βœ… API Key detected - Ready to chat!")
else:
    print("⚠️ No API Key found - Please set GROQ_API_KEY environment variable")

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0", 
        server_port=7860,
        show_error=True
    )