Spaces:
Sleeping
Sleeping
app.py
CHANGED
|
@@ -13,13 +13,14 @@ load_dotenv()
|
|
| 13 |
# Initialize client with API key from environment
|
| 14 |
initial_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
# Global variables to store API key and model
|
| 21 |
current_api_key = initial_api_key
|
| 22 |
current_model = "gpt-3.5-turbo"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
async def get_openai_response(prompt, model="gpt-3.5-turbo"):
|
| 25 |
"""Get response from OpenAI API"""
|
|
@@ -50,8 +51,9 @@ def chat_response(message, history, api_key, model):
|
|
| 50 |
|
| 51 |
# If client is still None, it means no valid API key has been set
|
| 52 |
if client is None:
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
# Update model if different
|
| 57 |
if model != current_model:
|
|
@@ -59,26 +61,27 @@ def chat_response(message, history, api_key, model):
|
|
| 59 |
|
| 60 |
# Check if API key is available
|
| 61 |
if not current_api_key:
|
| 62 |
-
|
| 63 |
-
history.append(
|
| 64 |
return history, ""
|
| 65 |
|
| 66 |
if not message.strip():
|
| 67 |
-
|
| 68 |
-
history.append(
|
| 69 |
return history, ""
|
| 70 |
|
| 71 |
# Add user message to history first
|
| 72 |
-
history.append(
|
|
|
|
| 73 |
|
| 74 |
# Get response from OpenAI
|
| 75 |
try:
|
| 76 |
response = asyncio.run(get_openai_response(message, current_model))
|
| 77 |
# Update the last message with the actual response
|
| 78 |
-
history[-1][
|
| 79 |
return history, ""
|
| 80 |
except Exception as e:
|
| 81 |
-
history[-1][
|
| 82 |
return history, ""
|
| 83 |
|
| 84 |
def clear_chat():
|
|
@@ -474,15 +477,15 @@ with gr.Blocks(css=custom_css, title="🤖 OpenAI Q&A Chatbot", theme=gr.themes.
|
|
| 474 |
|
| 475 |
with gr.Column(scale=2, elem_classes="fade-in-up"):
|
| 476 |
with gr.Group(elem_classes="chat-container"):
|
| 477 |
-
# Chat interface
|
| 478 |
chatbot = gr.Chatbot(
|
| 479 |
label="💬 **Conversation**",
|
| 480 |
height=450,
|
| 481 |
show_copy_button=True,
|
| 482 |
-
bubble_full_width=False,
|
| 483 |
show_label=True,
|
| 484 |
container=True,
|
| 485 |
-
scale=1
|
|
|
|
| 486 |
)
|
| 487 |
|
| 488 |
with gr.Row():
|
|
@@ -518,7 +521,7 @@ with gr.Blocks(css=custom_css, title="🤖 OpenAI Q&A Chatbot", theme=gr.themes.
|
|
| 518 |
2. **Select your preferred model** (GPT-3.5 recommended for speed)
|
| 519 |
3. **Start chatting** - type your question and hit send!
|
| 520 |
|
| 521 |
-
*Built
|
| 522 |
""")
|
| 523 |
|
| 524 |
# Event handlers with enhanced functionality
|
|
@@ -568,16 +571,16 @@ with gr.Blocks(css=custom_css, title="🤖 OpenAI Q&A Chatbot", theme=gr.themes.
|
|
| 568 |
# Auto-refresh status every 30 seconds
|
| 569 |
demo.load(refresh_status, outputs=[status_display])
|
| 570 |
|
| 571 |
-
# Launch with
|
| 572 |
if __name__ == "__main__":
|
| 573 |
demo.launch(
|
| 574 |
share=False,
|
| 575 |
-
server_name="0.0.0.0",
|
| 576 |
-
server_port=7861,
|
| 577 |
show_error=True,
|
| 578 |
show_api=False,
|
| 579 |
quiet=False,
|
| 580 |
-
|
| 581 |
-
|
|
|
|
|
|
|
| 582 |
ssl_verify=False
|
| 583 |
)
|
|
|
|
| 13 |
# Initialize client with API key from environment
|
| 14 |
initial_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Global variables to store API key and model
|
| 17 |
current_api_key = initial_api_key
|
| 18 |
current_model = "gpt-3.5-turbo"
|
| 19 |
+
client = None
|
| 20 |
+
|
| 21 |
+
# Initialize client if API key is available
|
| 22 |
+
if initial_api_key:
|
| 23 |
+
client = AsyncOpenAI(api_key=initial_api_key)
|
| 24 |
|
| 25 |
async def get_openai_response(prompt, model="gpt-3.5-turbo"):
|
| 26 |
"""Get response from OpenAI API"""
|
|
|
|
| 51 |
|
| 52 |
# If client is still None, it means no valid API key has been set
|
| 53 |
if client is None:
|
| 54 |
+
history.append({"role": "user", "content": message})
|
| 55 |
+
history.append({"role": "assistant", "content": "Please provide your OpenAI API key to start the conversation."})
|
| 56 |
+
return history, ""
|
| 57 |
|
| 58 |
# Update model if different
|
| 59 |
if model != current_model:
|
|
|
|
| 61 |
|
| 62 |
# Check if API key is available
|
| 63 |
if not current_api_key:
|
| 64 |
+
history.append({"role": "user", "content": message})
|
| 65 |
+
history.append({"role": "assistant", "content": "⚠️ Please enter your OpenAI API Key first."})
|
| 66 |
return history, ""
|
| 67 |
|
| 68 |
if not message.strip():
|
| 69 |
+
history.append({"role": "user", "content": message})
|
| 70 |
+
history.append({"role": "assistant", "content": "⚠️ Please enter a question."})
|
| 71 |
return history, ""
|
| 72 |
|
| 73 |
# Add user message to history first
|
| 74 |
+
history.append({"role": "user", "content": message})
|
| 75 |
+
history.append({"role": "assistant", "content": "🤔 Thinking..."})
|
| 76 |
|
| 77 |
# Get response from OpenAI
|
| 78 |
try:
|
| 79 |
response = asyncio.run(get_openai_response(message, current_model))
|
| 80 |
# Update the last message with the actual response
|
| 81 |
+
history[-1]["content"] = response
|
| 82 |
return history, ""
|
| 83 |
except Exception as e:
|
| 84 |
+
history[-1]["content"] = f"❌ Error: {str(e)}"
|
| 85 |
return history, ""
|
| 86 |
|
| 87 |
def clear_chat():
|
|
|
|
| 477 |
|
| 478 |
with gr.Column(scale=2, elem_classes="fade-in-up"):
|
| 479 |
with gr.Group(elem_classes="chat-container"):
|
| 480 |
+
# Chat interface - Fixed for newer Gradio versions
|
| 481 |
chatbot = gr.Chatbot(
|
| 482 |
label="💬 **Conversation**",
|
| 483 |
height=450,
|
| 484 |
show_copy_button=True,
|
|
|
|
| 485 |
show_label=True,
|
| 486 |
container=True,
|
| 487 |
+
scale=1,
|
| 488 |
+
type="messages" # Fixed: Use messages format instead of tuples
|
| 489 |
)
|
| 490 |
|
| 491 |
with gr.Row():
|
|
|
|
| 521 |
2. **Select your preferred model** (GPT-3.5 recommended for speed)
|
| 522 |
3. **Start chatting** - type your question and hit send!
|
| 523 |
|
| 524 |
+
*Built with ❤️ by Mahfujul Karim*
|
| 525 |
""")
|
| 526 |
|
| 527 |
# Event handlers with enhanced functionality
|
|
|
|
| 571 |
# Auto-refresh status every 30 seconds
|
| 572 |
demo.load(refresh_status, outputs=[status_display])
|
| 573 |
|
| 574 |
+
# Launch with Hugging Face Spaces compatible configuration
|
| 575 |
if __name__ == "__main__":
|
| 576 |
demo.launch(
|
| 577 |
share=False,
|
|
|
|
|
|
|
| 578 |
show_error=True,
|
| 579 |
show_api=False,
|
| 580 |
quiet=False,
|
| 581 |
+
# Remove server_name, server_port, and inbrowser for Hugging Face Spaces
|
| 582 |
+
# inbrowser=True,
|
| 583 |
+
# server_name="0.0.0.0",
|
| 584 |
+
# server_port=7861,
|
| 585 |
ssl_verify=False
|
| 586 |
)
|