Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,11 @@ import gradio as gr
|
|
| 9 |
from typing import List, Tuple
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
-
# 1. Load OpenAI API key
|
| 13 |
# -----------------------------
|
| 14 |
-
# IMPORTANT:
|
| 15 |
-
# Do NOT hardcode API keys for Spaces.
|
| 16 |
-
# Add OPENAI_API_KEY in Hugging Face → Settings → Secrets
|
| 17 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# -----------------------------
|
| 20 |
# 2. System prompt for chatbot
|
|
@@ -34,18 +33,12 @@ def reset() -> List[Tuple[str, str]]:
|
|
| 34 |
# 4. Chatbot function
|
| 35 |
# -----------------------------
|
| 36 |
def interact_chatbot(user_input: str, history: List[Tuple[str, str]], temp: float) -> List[Tuple[str, str]]:
|
| 37 |
-
"""
|
| 38 |
-
Maintains context: sends the conversation history + new user input to ChatGPT API
|
| 39 |
-
"""
|
| 40 |
messages = [{'role': 'system', 'content': system_prompt}]
|
| 41 |
-
|
| 42 |
for u, c in history:
|
| 43 |
messages.append({'role': 'user', 'content': u})
|
| 44 |
messages.append({'role': 'assistant', 'content': c})
|
| 45 |
-
|
| 46 |
messages.append({'role': 'user', 'content': user_input})
|
| 47 |
|
| 48 |
-
# Call the API
|
| 49 |
try:
|
| 50 |
response = openai.chat.completions.create(
|
| 51 |
model="gpt-3.5-turbo",
|
|
@@ -57,7 +50,6 @@ def interact_chatbot(user_input: str, history: List[Tuple[str, str]], temp: floa
|
|
| 57 |
except Exception as e:
|
| 58 |
assistant_reply = f"⚠️ Error: {str(e)}"
|
| 59 |
|
| 60 |
-
# Update history
|
| 61 |
history.append((user_input, assistant_reply))
|
| 62 |
return history
|
| 63 |
|
|
@@ -70,10 +62,12 @@ with gr.Blocks(css="""
|
|
| 70 |
h3 {color: #5a2d82;}
|
| 71 |
.footer {text-align: center; color: #666; font-size: 14px; margin-top: 20px;}
|
| 72 |
""") as demo:
|
|
|
|
| 73 |
gr.Markdown("# ✨ Conversational Chatbot with ChatGPT API ✨")
|
| 74 |
gr.Markdown("### Made with ❤️ by **Shruti Mandaokar**")
|
| 75 |
|
| 76 |
-
chatbot
|
|
|
|
| 77 |
user_input = gr.Textbox(label="Your message", placeholder="Type a sentence...")
|
| 78 |
|
| 79 |
with gr.Column():
|
|
@@ -98,4 +92,6 @@ with gr.Blocks(css="""
|
|
| 98 |
# 6. Launch the app
|
| 99 |
# -----------------------------
|
| 100 |
if __name__ == "__main__":
|
| 101 |
-
|
|
|
|
|
|
|
|
|
| 9 |
from typing import List, Tuple
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
+
# 1. Load OpenAI API key from Hugging Face Secrets
|
| 13 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
| 14 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
if openai.api_key is None:
|
| 16 |
+
raise ValueError("❌ OPENAI_API_KEY not found. Please add it in Hugging Face Secrets.")
|
| 17 |
|
| 18 |
# -----------------------------
|
| 19 |
# 2. System prompt for chatbot
|
|
|
|
| 33 |
# 4. Chatbot function
|
| 34 |
# -----------------------------
|
| 35 |
def interact_chatbot(user_input: str, history: List[Tuple[str, str]], temp: float) -> List[Tuple[str, str]]:
|
|
|
|
|
|
|
|
|
|
| 36 |
messages = [{'role': 'system', 'content': system_prompt}]
|
|
|
|
| 37 |
for u, c in history:
|
| 38 |
messages.append({'role': 'user', 'content': u})
|
| 39 |
messages.append({'role': 'assistant', 'content': c})
|
|
|
|
| 40 |
messages.append({'role': 'user', 'content': user_input})
|
| 41 |
|
|
|
|
| 42 |
try:
|
| 43 |
response = openai.chat.completions.create(
|
| 44 |
model="gpt-3.5-turbo",
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
assistant_reply = f"⚠️ Error: {str(e)}"
|
| 52 |
|
|
|
|
| 53 |
history.append((user_input, assistant_reply))
|
| 54 |
return history
|
| 55 |
|
|
|
|
| 62 |
h3 {color: #5a2d82;}
|
| 63 |
.footer {text-align: center; color: #666; font-size: 14px; margin-top: 20px;}
|
| 64 |
""") as demo:
|
| 65 |
+
|
| 66 |
gr.Markdown("# ✨ Conversational Chatbot with ChatGPT API ✨")
|
| 67 |
gr.Markdown("### Made with ❤️ by **Shruti Mandaokar**")
|
| 68 |
|
| 69 |
+
# Modern chatbot with role/content format
|
| 70 |
+
chatbot = gr.Chatbot(label="💬 Chatbot", type="messages")
|
| 71 |
user_input = gr.Textbox(label="Your message", placeholder="Type a sentence...")
|
| 72 |
|
| 73 |
with gr.Column():
|
|
|
|
| 92 |
# 6. Launch the app
|
| 93 |
# -----------------------------
|
| 94 |
if __name__ == "__main__":
|
| 95 |
+
# Detect if running on Hugging Face Spaces
|
| 96 |
+
on_spaces = os.getenv("SYSTEM") == "spaces"
|
| 97 |
+
demo.launch(share=not on_spaces)
|