Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =======================================================
|
| 2 |
+
# Conversational Chatbot with ChatGPT API + Gradio
|
| 3 |
+
# Author: Shruti Mandaokar
|
| 4 |
+
# =======================================================
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import openai
|
| 8 |
+
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
|
| 21 |
+
# -----------------------------
|
| 22 |
+
system_prompt = (
|
| 23 |
+
"You are a helpful assistant that corrects grammar and answers questions. "
|
| 24 |
+
"Maintain context of the conversation."
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# 3. Reset conversation history
|
| 29 |
+
# -----------------------------
|
| 30 |
+
def reset() -> List[Tuple[str, str]]:
|
| 31 |
+
return []
|
| 32 |
+
|
| 33 |
+
# -----------------------------
|
| 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",
|
| 52 |
+
messages=messages,
|
| 53 |
+
temperature=temp,
|
| 54 |
+
max_tokens=200
|
| 55 |
+
)
|
| 56 |
+
assistant_reply = response.choices[0].message.content
|
| 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 |
+
|
| 64 |
+
# -----------------------------
|
| 65 |
+
# 5. Gradio UI
|
| 66 |
+
# -----------------------------
|
| 67 |
+
with gr.Blocks(css="""
|
| 68 |
+
.gradio-container {background-color: #f0f4f8;}
|
| 69 |
+
h1 {color: #2b547e; text-align: center;}
|
| 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 = gr.Chatbot(label="💬 Chatbot")
|
| 77 |
+
user_input = gr.Textbox(label="Your message", placeholder="Type a sentence...")
|
| 78 |
+
|
| 79 |
+
with gr.Column():
|
| 80 |
+
gr.Markdown("### 🎨 Creativity Control")
|
| 81 |
+
temperature_slider = gr.Slider(0.0, 2.0, value=1.0, step=0.1, label="Temperature")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
send_button = gr.Button("🚀 Send")
|
| 85 |
+
reset_button = gr.Button("🔄 Reset Chat")
|
| 86 |
+
|
| 87 |
+
send_button.click(
|
| 88 |
+
interact_chatbot,
|
| 89 |
+
inputs=[user_input, chatbot, temperature_slider],
|
| 90 |
+
outputs=[chatbot]
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
reset_button.click(reset, outputs=[chatbot])
|
| 94 |
+
|
| 95 |
+
gr.Markdown('<div class="footer">🌸 Conversational Chatbot | Shruti Mandaokar 🌸</div>')
|
| 96 |
+
|
| 97 |
+
# -----------------------------
|
| 98 |
+
# 6. Launch the app
|
| 99 |
+
# -----------------------------
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
demo.launch(share=True)
|