Spaces:
Build error
Build error
Commit ·
37bb369
1
Parent(s): 8ffd026
full documentation and refactoring.
Browse files
app.py
CHANGED
|
@@ -3,45 +3,72 @@ import os
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import cohere
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 7 |
-
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
client_hf = InferenceClient(model=
|
| 11 |
-
client_cohere = cohere.Client(COHERE_API_KEY)
|
| 12 |
|
| 13 |
def respond(
|
| 14 |
-
message,
|
| 15 |
-
history: list[tuple[str, str]],
|
| 16 |
-
system_message,
|
| 17 |
-
max_tokens,
|
| 18 |
-
temperature,
|
| 19 |
-
top_p,
|
| 20 |
-
use_cohere
|
| 21 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
messages = [{"role": "system", "content": system_message}]
|
| 23 |
|
| 24 |
-
for
|
| 25 |
-
if
|
| 26 |
-
messages.append({"role": "user", "content":
|
| 27 |
-
if
|
| 28 |
-
messages.append({"role": "assistant", "content":
|
| 29 |
|
| 30 |
-
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
response = ""
|
| 33 |
|
| 34 |
-
if use_cohere:
|
|
|
|
| 35 |
cohere_response = client_cohere.chat(
|
| 36 |
message=message,
|
| 37 |
-
model=
|
| 38 |
temperature=temperature,
|
| 39 |
max_tokens=max_tokens
|
| 40 |
)
|
| 41 |
response = cohere_response.text
|
| 42 |
-
yield response # Yield full response
|
| 43 |
|
| 44 |
-
else:
|
|
|
|
| 45 |
for message in client_hf.chat_completion(
|
| 46 |
messages,
|
| 47 |
max_tokens=max_tokens,
|
|
@@ -49,21 +76,22 @@ def respond(
|
|
| 49 |
temperature=temperature,
|
| 50 |
top_p=top_p,
|
| 51 |
):
|
| 52 |
-
token = message.choices[0].delta.content
|
| 53 |
response += token
|
| 54 |
-
yield response
|
| 55 |
|
| 56 |
-
# Gradio UI
|
| 57 |
demo = gr.ChatInterface(
|
| 58 |
respond,
|
| 59 |
additional_inputs=[
|
| 60 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System
|
| 61 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 62 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 63 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 64 |
-
gr.Checkbox(label="Use Cohere
|
| 65 |
],
|
| 66 |
)
|
| 67 |
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import cohere
|
| 5 |
|
| 6 |
+
# Model & API setup
|
| 7 |
+
COHERE_MODEL = "command-r-plus"
|
| 8 |
+
HF_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 9 |
+
|
| 10 |
+
# Fetch API keys from environment variables
|
| 11 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
| 12 |
+
COHERE_API_KEY = os.getenv("COHERE_API_KEY")
|
| 13 |
|
| 14 |
+
# Initialize clients for Hugging Face and Cohere APIs
|
| 15 |
+
client_hf = InferenceClient(model=HF_MODEL, token=HF_API_KEY)
|
| 16 |
+
client_cohere = cohere.Client(COHERE_API_KEY)
|
| 17 |
|
| 18 |
def respond(
|
| 19 |
+
message: str,
|
| 20 |
+
history: list[tuple[str, str]],
|
| 21 |
+
system_message: str,
|
| 22 |
+
max_tokens: int,
|
| 23 |
+
temperature: float,
|
| 24 |
+
top_p: float,
|
| 25 |
+
use_cohere: bool
|
| 26 |
):
|
| 27 |
+
"""Handles chatbot responses based on user input and chat history.
|
| 28 |
+
|
| 29 |
+
This function integrates with either the Cohere API or Hugging Face API to generate AI-based responses.
|
| 30 |
+
|
| 31 |
+
Args:
|
| 32 |
+
message (str): The latest user message.
|
| 33 |
+
history (list[tuple[str, str]]): A list of previous exchanges where:
|
| 34 |
+
- Each tuple contains (user_message, assistant_response).
|
| 35 |
+
- Example: [("Hello", "Hi there!"), ("How are you?", "I'm good!")]
|
| 36 |
+
system_message (str): A system-level instruction for the chatbot (e.g., personality, style).
|
| 37 |
+
max_tokens (int): Maximum number of new tokens the model can generate.
|
| 38 |
+
temperature (float): Controls randomness (higher = more varied responses).
|
| 39 |
+
top_p (float): Probability threshold for token selection (higher = more diverse responses).
|
| 40 |
+
use_cohere (bool): If True, uses Cohere API; otherwise, uses Hugging Face API.
|
| 41 |
+
|
| 42 |
+
Yields:
|
| 43 |
+
str: The chatbot's response (streamed for Hugging Face, full response for Cohere).
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# Constructing the message history for context
|
| 47 |
messages = [{"role": "system", "content": system_message}]
|
| 48 |
|
| 49 |
+
for user_msg, assistant_msg in history:
|
| 50 |
+
if user_msg:
|
| 51 |
+
messages.append({"role": "user", "content": user_msg})
|
| 52 |
+
if assistant_msg:
|
| 53 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 54 |
|
| 55 |
+
messages.append({"role": "user", "content": message}) # Append current user message
|
| 56 |
|
| 57 |
response = ""
|
| 58 |
|
| 59 |
+
if use_cohere:
|
| 60 |
+
# Using Cohere API (no streaming support)
|
| 61 |
cohere_response = client_cohere.chat(
|
| 62 |
message=message,
|
| 63 |
+
model=COHERE_MODEL,
|
| 64 |
temperature=temperature,
|
| 65 |
max_tokens=max_tokens
|
| 66 |
)
|
| 67 |
response = cohere_response.text
|
| 68 |
+
yield response # Yield full response immediately
|
| 69 |
|
| 70 |
+
else:
|
| 71 |
+
# Using Hugging Face API (streaming responses)
|
| 72 |
for message in client_hf.chat_completion(
|
| 73 |
messages,
|
| 74 |
max_tokens=max_tokens,
|
|
|
|
| 76 |
temperature=temperature,
|
| 77 |
top_p=top_p,
|
| 78 |
):
|
| 79 |
+
token = message.choices[0].delta.content # Extract generated token
|
| 80 |
response += token
|
| 81 |
+
yield response # Yield response incrementally
|
| 82 |
|
| 83 |
+
# Gradio UI with user-configurable inputs
|
| 84 |
demo = gr.ChatInterface(
|
| 85 |
respond,
|
| 86 |
additional_inputs=[
|
| 87 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System prompt"), # System instruction
|
| 88 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), # Token limit
|
| 89 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), # Randomness control
|
| 90 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"), # Probability mass
|
| 91 |
+
gr.Checkbox(label="Use capable Cohere model instead."), # API selection toggle
|
| 92 |
],
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Start Gradio interface
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch()
|