Spaces:
Sleeping
Sleeping
Commit
·
396fb6e
1
Parent(s):
e7db4c3
readme fill
Browse files
README.md
CHANGED
@@ -1,13 +1,97 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Chatbot with Hugging Face Spaces & Gradio
|
2 |
+
|
3 |
+
This is an interactive chatbot deployed on **Hugging Face Spaces** using **Gradio**. It supports both **Cohere** and **Hugging Face** models, allowing users to select between them for generating responses.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
✅ **Supports two AI models:**
|
7 |
+
- Cohere API (`command-r-plus`)
|
8 |
+
- Hugging Face API (`mistralai/Mistral-7B-Instruct-v0.3`)
|
9 |
+
✅ **Customizable Settings:**
|
10 |
+
- System prompt
|
11 |
+
- Max tokens
|
12 |
+
- Temperature
|
13 |
+
- Top-p value
|
14 |
+
✅ **Streaming responses** (for Hugging Face models)
|
15 |
+
✅ **Gradio-powered UI** for easy interaction
|
16 |
+
|
17 |
+
## Installation & Setup
|
18 |
+
|
19 |
+
### 1️⃣ Clone the Repository
|
20 |
+
```bash
|
21 |
+
git clone https://huggingface.co/spaces/your-space-name
|
22 |
+
cd your-space-name
|
23 |
+
```
|
24 |
+
|
25 |
+
### 2️⃣ Install Dependencies
|
26 |
+
Make sure you have Python installed, then run:
|
27 |
+
```bash
|
28 |
+
pip install -r requirements.txt
|
29 |
+
```
|
30 |
+
|
31 |
+
### 3️⃣ Set API Keys
|
32 |
+
You need to set up API keys for **Hugging Face** and **Cohere**. You can do this via environment variables:
|
33 |
+
```bash
|
34 |
+
export HF_API_KEY='your-huggingface-api-key'
|
35 |
+
export COHERE_API_KEY='your-cohere-api-key'
|
36 |
+
```
|
37 |
+
|
38 |
+
### 4️⃣ Run the App Locally
|
39 |
+
```bash
|
40 |
+
python app.py
|
41 |
+
```
|
42 |
+
This will launch the Gradio interface in your browser.
|
43 |
+
|
44 |
+
## Deployment on Hugging Face Spaces
|
45 |
+
1. Create a new **Space** on Hugging Face.
|
46 |
+
2. Choose **Gradio** as the framework.
|
47 |
+
3. Upload `app.py` and `requirements.txt`.
|
48 |
+
4. Deploy and test the chatbot.
|
49 |
+
|
50 |
+
## Usage
|
51 |
+
1. Enter your message in the chatbox.
|
52 |
+
2. Choose the AI model (Hugging Face or Cohere).
|
53 |
+
3. Adjust chatbot parameters as needed.
|
54 |
+
4. Receive and interact with AI-generated responses.
|
55 |
+
|
56 |
+
## Code Overview
|
57 |
+
|
58 |
+
### API Clients
|
59 |
+
```python
|
60 |
+
from huggingface_hub import InferenceClient
|
61 |
+
import cohere
|
62 |
+
|
63 |
+
client_hf = InferenceClient(model='mistralai/Mistral-7B-Instruct-v0.3', token=HF_API_KEY)
|
64 |
+
client_cohere = cohere.Client(COHERE_API_KEY)
|
65 |
+
```
|
66 |
+
|
67 |
+
### Chatbot Function
|
68 |
+
```python
|
69 |
+
def respond(message: str, history: list, system_message: str, max_tokens: int, temperature: float, top_p: float, use_cohere: bool):
|
70 |
+
messages = [{"role": "system", "content": system_message}]
|
71 |
+
for val in history:
|
72 |
+
messages.append({"role": "user", "content": val[0]})
|
73 |
+
messages.append({"role": "assistant", "content": val[1]})
|
74 |
+
messages.append({"role": "user", "content": message})
|
75 |
+
```
|
76 |
+
|
77 |
+
### Gradio UI Setup
|
78 |
+
```python
|
79 |
+
demo = gr.ChatInterface(
|
80 |
+
respond,
|
81 |
+
additional_inputs=[
|
82 |
+
gr.Textbox(value='You are a friendly Chatbot.', label='System prompt'),
|
83 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label='Max new tokens'),
|
84 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label='Temperature'),
|
85 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label='Top-p'),
|
86 |
+
gr.Checkbox(label='Use Cohere model instead.'),
|
87 |
+
],
|
88 |
+
)
|
89 |
+
```
|
90 |
+
|
91 |
+
## License
|
92 |
+
This project is licensed under the MIT License.
|
93 |
+
|
94 |
+
## Author
|
95 |
+
👤 **Your Name**
|
96 |
+
📧 Contact: [email protected]
|
97 |
+
|