WillemVH commited on
Commit
3d109b8
·
verified ·
1 Parent(s): 33206e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import re
4
+
5
+ class GroqChatAssistant:
6
+ def __init__(self):
7
+ self.api_key = None
8
+ self.api_url = "https://api.groq.com/openai/v1/chat/completions"
9
+ self.conversation_history = []
10
+ self.system_prompt = """You are a helpful ai that ends every message with <STOP>"""
11
+
12
+ def set_key(self, api_key):
13
+ if api_key.startswith("gsk_") and len(api_key) > 30:
14
+ self.api_key = api_key
15
+ return "API key set successfully!"
16
+ return "Invalid API key format"
17
+
18
+ def add_to_history(self, role, content):
19
+ cleaned = re.sub(r'<STOP>|</?think>', '', content, flags=re.DOTALL).strip()
20
+ self.conversation_history.append({"role": role, "content": cleaned})
21
+
22
+ def reset_history(self):
23
+ self.conversation_history = []
24
+ return "Conversation history reset"
25
+
26
+ def get_response(self, user_input):
27
+ if not self.api_key:
28
+ return "API key not set. Use the 'Set Key' tab."
29
+
30
+ self.add_to_history("user", user_input)
31
+
32
+ try:
33
+ response = requests.post(
34
+ self.api_url,
35
+ headers={
36
+ "Authorization": f"Bearer {self.api_key}",
37
+ "Content-Type": "application/json"
38
+ },
39
+ json={
40
+ "model": "llama3-70b-8192",
41
+ "messages": [
42
+ {"role": "system", "content": self.system_prompt},
43
+ *self.conversation_history
44
+ ],
45
+ "temperature": 0.7,
46
+ "stop": ["<STOP>"]
47
+ }
48
+ )
49
+ response.raise_for_status()
50
+ full_response = response.json()["choices"][0]["message"]["content"]
51
+ clean_response = re.sub(r'<STOP>.*', '', full_response).strip()
52
+ self.add_to_history("assistant", clean_response)
53
+ return clean_response
54
+ except Exception as e:
55
+ return f"Error: {str(e)}"
56
+
57
+ assistant = GroqChatAssistant()
58
+
59
+ # Gradio Interface
60
+ with gr.Blocks(title="AI Chat") as app:
61
+ gr.Markdown("# 🤖 AI Chat (Groq API)")
62
+
63
+ with gr.Tab("Chat"):
64
+ chat_input = gr.Textbox(label="Your Message")
65
+ chat_output = gr.Textbox(label="AI's Response", interactive=False)
66
+ chat_button = gr.Button("Send")
67
+
68
+ def chat(user_input):
69
+ return assistant.get_response(user_input)
70
+
71
+ chat_button.click(chat, inputs=chat_input, outputs=chat_output)
72
+
73
+ with gr.Tab("Set API Key"):
74
+ key_input = gr.Textbox(label="Groq API Key (starts with 'gsk_')", type="password")
75
+ key_output = gr.Textbox(label="Status", interactive=False)
76
+ key_button = gr.Button("Set Key")
77
+
78
+ key_button.click(assistant.set_key, inputs=key_input, outputs=key_output)
79
+
80
+ with gr.Tab("Reset"):
81
+ reset_output = gr.Textbox(label="Status", interactive=False)
82
+ reset_button = gr.Button("Reset Conversation")
83
+
84
+ reset_button.click(assistant.reset_history, outputs=reset_output)
85
+
86
+ app.launch()