Spaces:
Configuration error
Configuration error
Upload 3 files
Browse files- README (1).md +48 -0
- app.py +83 -0
- requirements.txt +2 -0
README (1).md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AI Chatbot
|
| 3 |
+
emoji: π€
|
| 4 |
+
colorFrom: violet
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.44.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# AI Chatbot β Powered by Claude
|
| 13 |
+
|
| 14 |
+
A clean, streaming AI chatbot built with [Gradio](https://gradio.app) and [Anthropic's Claude](https://www.anthropic.com).
|
| 15 |
+
|
| 16 |
+
## π Deploy on Hugging Face Spaces
|
| 17 |
+
|
| 18 |
+
1. **Create a new Space** at https://huggingface.co/new-space
|
| 19 |
+
- SDK: **Gradio**
|
| 20 |
+
- Visibility: Public or Private
|
| 21 |
+
|
| 22 |
+
2. **Upload these files** (`app.py`, `requirements.txt`, `README.md`) to the Space repo.
|
| 23 |
+
|
| 24 |
+
3. **Add your API key** as a Secret:
|
| 25 |
+
- Go to your Space β **Settings** β **Variables and secrets**
|
| 26 |
+
- Add a secret named `ANTHROPIC_API_KEY` with your key from https://console.anthropic.com
|
| 27 |
+
|
| 28 |
+
4. The Space will build automatically and your chatbot will be live! π
|
| 29 |
+
|
| 30 |
+
## π» Run Locally
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
# Install dependencies
|
| 34 |
+
pip install -r requirements.txt
|
| 35 |
+
|
| 36 |
+
# Set your API key
|
| 37 |
+
export ANTHROPIC_API_KEY=your_key_here # Mac/Linux
|
| 38 |
+
set ANTHROPIC_API_KEY=your_key_here # Windows
|
| 39 |
+
|
| 40 |
+
# Launch
|
| 41 |
+
python app.py
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
Then open http://localhost:7860 in your browser.
|
| 45 |
+
|
| 46 |
+
## βοΈ Customisation
|
| 47 |
+
|
| 48 |
+
Edit the `SYSTEM_PROMPT` variable in `app.py` to give your chatbot a custom personality or role.
|
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import anthropic
|
| 4 |
+
|
| 5 |
+
client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
| 6 |
+
|
| 7 |
+
SYSTEM_PROMPT = """You are a helpful, friendly, and knowledgeable AI assistant.
|
| 8 |
+
You answer questions clearly and concisely, and you're always eager to help."""
|
| 9 |
+
|
| 10 |
+
def chat(message, history):
|
| 11 |
+
"""Send message to Claude and stream the response."""
|
| 12 |
+
messages = []
|
| 13 |
+
for user_msg, assistant_msg in history:
|
| 14 |
+
messages.append({"role": "user", "content": user_msg})
|
| 15 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 16 |
+
messages.append({"role": "user", "content": message})
|
| 17 |
+
|
| 18 |
+
response_text = ""
|
| 19 |
+
with client.messages.stream(
|
| 20 |
+
model="claude-sonnet-4-6",
|
| 21 |
+
max_tokens=1024,
|
| 22 |
+
system=SYSTEM_PROMPT,
|
| 23 |
+
messages=messages,
|
| 24 |
+
) as stream:
|
| 25 |
+
for text in stream.text_stream:
|
| 26 |
+
response_text += text
|
| 27 |
+
yield response_text
|
| 28 |
+
|
| 29 |
+
# ββ UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 30 |
+
with gr.Blocks(
|
| 31 |
+
title="AI Chatbot",
|
| 32 |
+
theme=gr.themes.Soft(primary_hue="violet"),
|
| 33 |
+
css="""
|
| 34 |
+
#chatbot { height: 520px; }
|
| 35 |
+
footer { display: none !important; }
|
| 36 |
+
""",
|
| 37 |
+
) as demo:
|
| 38 |
+
gr.Markdown(
|
| 39 |
+
"""
|
| 40 |
+
# π€ AI Chatbot
|
| 41 |
+
Powered by Claude β ask me anything!
|
| 42 |
+
"""
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
chatbot = gr.Chatbot(
|
| 46 |
+
elem_id="chatbot",
|
| 47 |
+
bubble_full_width=False,
|
| 48 |
+
show_label=False,
|
| 49 |
+
avatar_images=(None, "https://www.anthropic.com/favicon.ico"),
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
msg = gr.Textbox(
|
| 54 |
+
placeholder="Type your message hereβ¦",
|
| 55 |
+
show_label=False,
|
| 56 |
+
scale=9,
|
| 57 |
+
container=False,
|
| 58 |
+
)
|
| 59 |
+
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 60 |
+
|
| 61 |
+
clear_btn = gr.ClearButton([msg, chatbot], value="ποΈ Clear Chat")
|
| 62 |
+
|
| 63 |
+
def user_submit(message, history):
|
| 64 |
+
return "", history + [[message, None]]
|
| 65 |
+
|
| 66 |
+
def bot_respond(history):
|
| 67 |
+
user_message = history[-1][0]
|
| 68 |
+
prior_history = history[:-1]
|
| 69 |
+
history[-1][1] = ""
|
| 70 |
+
for chunk in chat(user_message, prior_history):
|
| 71 |
+
history[-1][1] = chunk
|
| 72 |
+
yield history
|
| 73 |
+
|
| 74 |
+
msg.submit(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 75 |
+
bot_respond, chatbot, chatbot
|
| 76 |
+
)
|
| 77 |
+
send_btn.click(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 78 |
+
bot_respond, chatbot, chatbot
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.queue()
|
| 83 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
anthropic>=0.34.0
|
| 2 |
+
gradio>=4.44.0
|