Update app.py
Browse files
app.py
CHANGED
@@ -17,14 +17,12 @@ class ChatbotManager:
|
|
17 |
self.temperature = 0.7
|
18 |
|
19 |
def set_api_key(self, api_key: str) -> str:
|
20 |
-
"""Set the OpenAI API key"""
|
21 |
if not api_key.strip():
|
22 |
return "β Please enter a valid API key"
|
23 |
|
24 |
self.current_api_key = api_key.strip()
|
25 |
openai.api_key = self.current_api_key
|
26 |
|
27 |
-
# Test the API key
|
28 |
try:
|
29 |
openai.Model.list()
|
30 |
return "β
API key validated successfully!"
|
@@ -32,7 +30,6 @@ class ChatbotManager:
|
|
32 |
return f"β Invalid API key: {str(e)}"
|
33 |
|
34 |
def update_settings(self, model: str, system_prompt: str, max_tokens: int, temperature: float) -> str:
|
35 |
-
"""Update chatbot settings"""
|
36 |
self.current_model = model
|
37 |
self.system_prompt = system_prompt
|
38 |
self.max_tokens = max_tokens
|
@@ -40,17 +37,14 @@ class ChatbotManager:
|
|
40 |
return f"β
Settings updated: Model={model}, Max Tokens={max_tokens}, Temperature={temperature}"
|
41 |
|
42 |
def preprocess_data(self, data_text: str) -> str:
|
43 |
-
"""Preprocess and integrate custom data into the system prompt"""
|
44 |
if not data_text.strip():
|
45 |
return "No custom data provided"
|
46 |
|
47 |
-
# Reset system prompt to avoid accumulation
|
48 |
base_prompt = "You are a helpful AI assistant. Respond in a friendly and informative manner."
|
49 |
self.system_prompt = base_prompt + f"\n\nAdditional Context:\n{data_text}"
|
50 |
return f"β
Custom data integrated ({len(data_text)} characters)"
|
51 |
|
52 |
def generate_response(self, user_input: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
53 |
-
"""Generate response using the selected LLM model"""
|
54 |
if not self.current_api_key:
|
55 |
return "β Please set your API key first!", history
|
56 |
|
@@ -58,18 +52,14 @@ class ChatbotManager:
|
|
58 |
return "Please enter a message.", history
|
59 |
|
60 |
try:
|
61 |
-
# Prepare conversation context
|
62 |
messages = [{"role": "system", "content": self.system_prompt}]
|
63 |
|
64 |
-
# Add conversation history
|
65 |
for user_msg, assistant_msg in history:
|
66 |
messages.append({"role": "user", "content": user_msg})
|
67 |
messages.append({"role": "assistant", "content": assistant_msg})
|
68 |
|
69 |
-
# Add current user input
|
70 |
messages.append({"role": "user", "content": user_input})
|
71 |
|
72 |
-
# Generate response using OpenAI 0.28 syntax
|
73 |
response = openai.ChatCompletion.create(
|
74 |
model=self.current_model,
|
75 |
messages=messages,
|
@@ -80,8 +70,6 @@ class ChatbotManager:
|
|
80 |
)
|
81 |
|
82 |
assistant_response = response.choices[0].message.content.strip()
|
83 |
-
|
84 |
-
# Update history
|
85 |
history.append((user_input, assistant_response))
|
86 |
|
87 |
return assistant_response, history
|
@@ -91,12 +79,10 @@ class ChatbotManager:
|
|
91 |
return error_msg, history
|
92 |
|
93 |
def clear_conversation(self) -> Tuple[str, List[Tuple[str, str]]]:
|
94 |
-
"""Clear conversation history"""
|
95 |
self.conversation_history = []
|
96 |
return "", []
|
97 |
|
98 |
def export_conversation(self, history: List[Tuple[str, str]]) -> str:
|
99 |
-
"""Export conversation history to JSON format"""
|
100 |
if not history:
|
101 |
return "No conversation to export"
|
102 |
|
@@ -112,9 +98,12 @@ class ChatbotManager:
|
|
112 |
filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
113 |
|
114 |
try:
|
115 |
-
|
|
|
|
|
|
|
116 |
json.dump(export_data, f, indent=2, ensure_ascii=False)
|
117 |
-
return f"β
Conversation exported to {filename}"
|
118 |
except Exception as e:
|
119 |
return f"β Export failed: {str(e)}"
|
120 |
|
@@ -132,12 +121,9 @@ AVAILABLE_MODELS = [
|
|
132 |
]
|
133 |
|
134 |
def create_interface():
|
135 |
-
"""Create the Gradio interface"""
|
136 |
-
|
137 |
with gr.Blocks(title="LLM-Based Chatbot", theme=gr.themes.Soft()) as demo:
|
138 |
gr.Markdown("""
|
139 |
# π€ LLM-Based Conversational AI Chatbot
|
140 |
-
|
141 |
This chatbot leverages powerful Language Models to provide intelligent conversations.
|
142 |
Enter your OpenAI API key to get started!
|
143 |
""")
|
@@ -149,10 +135,10 @@ def create_interface():
|
|
149 |
label="Conversation",
|
150 |
height=400,
|
151 |
show_label=True,
|
152 |
-
avatar_images=("
|
|
|
153 |
show_copy_button=True,
|
154 |
bubble_full_width=False,
|
155 |
-
show_share_button=True
|
156 |
)
|
157 |
|
158 |
with gr.Row():
|
@@ -191,7 +177,7 @@ def create_interface():
|
|
191 |
|
192 |
max_tokens_slider = gr.Slider(
|
193 |
minimum=50,
|
194 |
-
maximum=
|
195 |
value=150,
|
196 |
step=10,
|
197 |
label="π Max Tokens"
|
@@ -205,7 +191,6 @@ def create_interface():
|
|
205 |
label="π‘οΈ Temperature"
|
206 |
)
|
207 |
|
208 |
-
# Live settings display
|
209 |
gr.Markdown("### π Current Settings")
|
210 |
current_settings = gr.Textbox(
|
211 |
value="Model: gpt-3.5-turbo\nTokens: 150\nTemp: 0.7",
|
@@ -240,7 +225,6 @@ def create_interface():
|
|
240 |
interactive=False
|
241 |
)
|
242 |
|
243 |
-
# Preset system prompts
|
244 |
gr.Markdown("### π Preset System Prompts")
|
245 |
with gr.Row():
|
246 |
preset_customer_support = gr.Button("π₯ Customer Support", variant="secondary")
|
@@ -259,7 +243,7 @@ def create_interface():
|
|
259 |
|
260 |
### 2. **Configure Settings**
|
261 |
- **Model**: Choose from available GPT models
|
262 |
-
- **Max Tokens**: Control response length (50-
|
263 |
- **Temperature**: Adjust creativity (0.0 = focused, 1.0 = creative)
|
264 |
|
265 |
### 3. **Advanced Customization**
|
@@ -270,7 +254,7 @@ def create_interface():
|
|
270 |
### 4. **Chat Features**
|
271 |
- Type messages and get intelligent responses
|
272 |
- Clear conversation history anytime
|
273 |
-
- Export chat history as JSON
|
274 |
- Regenerate the last response
|
275 |
- Copy responses using the copy button
|
276 |
|
@@ -281,7 +265,7 @@ def create_interface():
|
|
281 |
- **Custom data integration**: Enhance responses with your own data
|
282 |
- **Export functionality**: Save conversations for later analysis
|
283 |
- **Real-time validation**: API key and settings verification
|
284 |
-
- **Visual indicators**: User
|
285 |
|
286 |
## π‘ Use Cases
|
287 |
|
@@ -316,7 +300,7 @@ def create_interface():
|
|
316 |
if not user_input.strip():
|
317 |
return history, ""
|
318 |
|
319 |
-
response, updated_history = chatbot.generate_response(user_input, history)
|
320 |
return updated_history, ""
|
321 |
|
322 |
def handle_settings_update(model, system_prompt, max_tokens, temperature):
|
@@ -332,16 +316,14 @@ def create_interface():
|
|
332 |
return chatbot.clear_conversation()
|
333 |
|
334 |
def handle_export(history):
|
335 |
-
return chatbot.export_conversation(history)
|
336 |
|
337 |
def handle_regenerate(history):
|
338 |
if not history:
|
339 |
-
return history
|
340 |
|
341 |
-
# Get the last user message and regenerate response
|
342 |
last_user_msg = history[-1][0]
|
343 |
history_without_last = history[:-1]
|
344 |
-
|
345 |
response, updated_history = chatbot.generate_response(last_user_msg, history_without_last)
|
346 |
return updated_history
|
347 |
|
@@ -409,7 +391,6 @@ def create_interface():
|
|
409 |
outputs=[chatbot_interface]
|
410 |
)
|
411 |
|
412 |
-
# Live settings update
|
413 |
for component in [model_dropdown, max_tokens_slider, temperature_slider]:
|
414 |
component.change(
|
415 |
update_settings_display,
|
@@ -417,7 +398,6 @@ def create_interface():
|
|
417 |
outputs=[current_settings]
|
418 |
)
|
419 |
|
420 |
-
# Reset and preset buttons
|
421 |
reset_prompt_btn.click(
|
422 |
reset_prompt,
|
423 |
outputs=[system_prompt_input, settings_status]
|
@@ -445,41 +425,11 @@ def create_interface():
|
|
445 |
|
446 |
return demo
|
447 |
|
448 |
-
# Requirements and setup instructions
|
449 |
-
def print_setup_instructions():
|
450 |
-
"""Print setup instructions"""
|
451 |
-
print("""
|
452 |
-
π€ LLM-Based Chatbot Setup Instructions
|
453 |
-
=====================================
|
454 |
-
|
455 |
-
π¦ Required Dependencies:
|
456 |
-
pip install openai==0.28.0
|
457 |
-
pip install gradio
|
458 |
-
pip install requests
|
459 |
-
|
460 |
-
π API Key Setup:
|
461 |
-
1. Visit https://platform.openai.com/
|
462 |
-
2. Create an account and generate an API key
|
463 |
-
3. Enter the API key in the interface
|
464 |
-
|
465 |
-
π Running the Application:
|
466 |
-
python app.py
|
467 |
-
|
468 |
-
π Files Created:
|
469 |
-
- conversation_YYYYMMDD_HHMMSS.json (exported chats)
|
470 |
-
|
471 |
-
π Access: http://localhost:7860
|
472 |
-
""")
|
473 |
-
|
474 |
if __name__ == "__main__":
|
475 |
-
print_setup_instructions()
|
476 |
-
|
477 |
# Create and launch the interface
|
478 |
demo = create_interface()
|
479 |
|
480 |
-
# Launch with
|
481 |
demo.launch(
|
482 |
-
share=True
|
483 |
-
debug=True,
|
484 |
-
show_error=True
|
485 |
)
|
|
|
17 |
self.temperature = 0.7
|
18 |
|
19 |
def set_api_key(self, api_key: str) -> str:
|
|
|
20 |
if not api_key.strip():
|
21 |
return "β Please enter a valid API key"
|
22 |
|
23 |
self.current_api_key = api_key.strip()
|
24 |
openai.api_key = self.current_api_key
|
25 |
|
|
|
26 |
try:
|
27 |
openai.Model.list()
|
28 |
return "β
API key validated successfully!"
|
|
|
30 |
return f"β Invalid API key: {str(e)}"
|
31 |
|
32 |
def update_settings(self, model: str, system_prompt: str, max_tokens: int, temperature: float) -> str:
|
|
|
33 |
self.current_model = model
|
34 |
self.system_prompt = system_prompt
|
35 |
self.max_tokens = max_tokens
|
|
|
37 |
return f"β
Settings updated: Model={model}, Max Tokens={max_tokens}, Temperature={temperature}"
|
38 |
|
39 |
def preprocess_data(self, data_text: str) -> str:
|
|
|
40 |
if not data_text.strip():
|
41 |
return "No custom data provided"
|
42 |
|
|
|
43 |
base_prompt = "You are a helpful AI assistant. Respond in a friendly and informative manner."
|
44 |
self.system_prompt = base_prompt + f"\n\nAdditional Context:\n{data_text}"
|
45 |
return f"β
Custom data integrated ({len(data_text)} characters)"
|
46 |
|
47 |
def generate_response(self, user_input: str, history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
|
|
|
48 |
if not self.current_api_key:
|
49 |
return "β Please set your API key first!", history
|
50 |
|
|
|
52 |
return "Please enter a message.", history
|
53 |
|
54 |
try:
|
|
|
55 |
messages = [{"role": "system", "content": self.system_prompt}]
|
56 |
|
|
|
57 |
for user_msg, assistant_msg in history:
|
58 |
messages.append({"role": "user", "content": user_msg})
|
59 |
messages.append({"role": "assistant", "content": assistant_msg})
|
60 |
|
|
|
61 |
messages.append({"role": "user", "content": user_input})
|
62 |
|
|
|
63 |
response = openai.ChatCompletion.create(
|
64 |
model=self.current_model,
|
65 |
messages=messages,
|
|
|
70 |
)
|
71 |
|
72 |
assistant_response = response.choices[0].message.content.strip()
|
|
|
|
|
73 |
history.append((user_input, assistant_response))
|
74 |
|
75 |
return assistant_response, history
|
|
|
79 |
return error_msg, history
|
80 |
|
81 |
def clear_conversation(self) -> Tuple[str, List[Tuple[str, str]]]:
|
|
|
82 |
self.conversation_history = []
|
83 |
return "", []
|
84 |
|
85 |
def export_conversation(self, history: List[Tuple[str, str]]) -> str:
|
|
|
86 |
if not history:
|
87 |
return "No conversation to export"
|
88 |
|
|
|
98 |
filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
99 |
|
100 |
try:
|
101 |
+
# For Hugging Face Spaces, use /tmp directory for writing files
|
102 |
+
os.makedirs("/tmp", exist_ok=True)
|
103 |
+
filepath = os.path.join("/tmp", filename)
|
104 |
+
with open(filepath, 'w', encoding='utf-8') as f:
|
105 |
json.dump(export_data, f, indent=2, ensure_ascii=False)
|
106 |
+
return f"β
Conversation exported to {filename}. Download it from the file explorer."
|
107 |
except Exception as e:
|
108 |
return f"β Export failed: {str(e)}"
|
109 |
|
|
|
121 |
]
|
122 |
|
123 |
def create_interface():
|
|
|
|
|
124 |
with gr.Blocks(title="LLM-Based Chatbot", theme=gr.themes.Soft()) as demo:
|
125 |
gr.Markdown("""
|
126 |
# π€ LLM-Based Conversational AI Chatbot
|
|
|
127 |
This chatbot leverages powerful Language Models to provide intelligent conversations.
|
128 |
Enter your OpenAI API key to get started!
|
129 |
""")
|
|
|
135 |
label="Conversation",
|
136 |
height=400,
|
137 |
show_label=True,
|
138 |
+
avatar_images=("https://huggingface.co/spaces/gradio/theme-assets/resolve/main/user.png",
|
139 |
+
"https://huggingface.co/spaces/gradio/theme-assets/resolve/main/assistant.png"),
|
140 |
show_copy_button=True,
|
141 |
bubble_full_width=False,
|
|
|
142 |
)
|
143 |
|
144 |
with gr.Row():
|
|
|
177 |
|
178 |
max_tokens_slider = gr.Slider(
|
179 |
minimum=50,
|
180 |
+
maximum=4096,
|
181 |
value=150,
|
182 |
step=10,
|
183 |
label="π Max Tokens"
|
|
|
191 |
label="π‘οΈ Temperature"
|
192 |
)
|
193 |
|
|
|
194 |
gr.Markdown("### π Current Settings")
|
195 |
current_settings = gr.Textbox(
|
196 |
value="Model: gpt-3.5-turbo\nTokens: 150\nTemp: 0.7",
|
|
|
225 |
interactive=False
|
226 |
)
|
227 |
|
|
|
228 |
gr.Markdown("### π Preset System Prompts")
|
229 |
with gr.Row():
|
230 |
preset_customer_support = gr.Button("π₯ Customer Support", variant="secondary")
|
|
|
243 |
|
244 |
### 2. **Configure Settings**
|
245 |
- **Model**: Choose from available GPT models
|
246 |
+
- **Max Tokens**: Control response length (50-4096)
|
247 |
- **Temperature**: Adjust creativity (0.0 = focused, 1.0 = creative)
|
248 |
|
249 |
### 3. **Advanced Customization**
|
|
|
254 |
### 4. **Chat Features**
|
255 |
- Type messages and get intelligent responses
|
256 |
- Clear conversation history anytime
|
257 |
+
- Export chat history as JSON (saved in /tmp directory)
|
258 |
- Regenerate the last response
|
259 |
- Copy responses using the copy button
|
260 |
|
|
|
265 |
- **Custom data integration**: Enhance responses with your own data
|
266 |
- **Export functionality**: Save conversations for later analysis
|
267 |
- **Real-time validation**: API key and settings verification
|
268 |
+
- **Visual indicators**: User and AI avatars
|
269 |
|
270 |
## π‘ Use Cases
|
271 |
|
|
|
300 |
if not user_input.strip():
|
301 |
return history, ""
|
302 |
|
303 |
+
response, updated_history = chatbot.generate_response(user_input, history or [])
|
304 |
return updated_history, ""
|
305 |
|
306 |
def handle_settings_update(model, system_prompt, max_tokens, temperature):
|
|
|
316 |
return chatbot.clear_conversation()
|
317 |
|
318 |
def handle_export(history):
|
319 |
+
return chatbot.export_conversation(history or [])
|
320 |
|
321 |
def handle_regenerate(history):
|
322 |
if not history:
|
323 |
+
return history or []
|
324 |
|
|
|
325 |
last_user_msg = history[-1][0]
|
326 |
history_without_last = history[:-1]
|
|
|
327 |
response, updated_history = chatbot.generate_response(last_user_msg, history_without_last)
|
328 |
return updated_history
|
329 |
|
|
|
391 |
outputs=[chatbot_interface]
|
392 |
)
|
393 |
|
|
|
394 |
for component in [model_dropdown, max_tokens_slider, temperature_slider]:
|
395 |
component.change(
|
396 |
update_settings_display,
|
|
|
398 |
outputs=[current_settings]
|
399 |
)
|
400 |
|
|
|
401 |
reset_prompt_btn.click(
|
402 |
reset_prompt,
|
403 |
outputs=[system_prompt_input, settings_status]
|
|
|
425 |
|
426 |
return demo
|
427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
428 |
if __name__ == "__main__":
|
|
|
|
|
429 |
# Create and launch the interface
|
430 |
demo = create_interface()
|
431 |
|
432 |
+
# Launch with settings suitable for Hugging Face Spaces
|
433 |
demo.launch(
|
434 |
+
share=True
|
|
|
|
|
435 |
)
|