Spaces:
Runtime error
Runtime error
Threatthriver
commited on
Commit
•
f9e8b5c
1
Parent(s):
880d4f5
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import logging
|
4 |
-
|
|
|
5 |
|
6 |
# Initialize the InferenceClient with the model ID from Hugging Face
|
7 |
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
@@ -13,18 +14,27 @@ logging.basicConfig(
|
|
13 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
14 |
)
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def log_conversation(user_message, bot_response):
|
17 |
-
"""
|
18 |
-
Logs the conversation between the user and the AI.
|
19 |
-
|
20 |
-
Args:
|
21 |
-
user_message (str): The user's input message.
|
22 |
-
bot_response (str): The AI's response.
|
23 |
-
"""
|
24 |
logging.info(f"User: {user_message}")
|
25 |
logging.info(f"Bot: {bot_response}")
|
26 |
|
27 |
def respond(
|
|
|
28 |
message: str,
|
29 |
history: list[tuple[str, str]],
|
30 |
system_message: str,
|
@@ -34,22 +44,12 @@ def respond(
|
|
34 |
stop_sequence: str,
|
35 |
stream_response: bool,
|
36 |
):
|
37 |
-
"""
|
38 |
-
Generates a response from the AI model based on the user's message and chat history.
|
39 |
-
|
40 |
-
Args:
|
41 |
-
message (str): The user's input message.
|
42 |
-
history (list): A list of tuples representing the conversation history (user, assistant).
|
43 |
-
system_message (str): A system-level message guiding the AI's behavior.
|
44 |
-
max_tokens (int): The maximum number of tokens for the output.
|
45 |
-
temperature (float): Sampling temperature for controlling the randomness.
|
46 |
-
top_p (float): Top-p (nucleus sampling) for controlling diversity.
|
47 |
-
stop_sequence (str): A custom stop sequence to end the response generation.
|
48 |
-
stream_response (bool): Whether to stream the response or return it as a whole.
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
53 |
|
54 |
# Prepare the conversation history for the API call
|
55 |
messages = [{"role": "system", "content": system_message}]
|
@@ -95,7 +95,7 @@ def respond(
|
|
95 |
yield response
|
96 |
|
97 |
except Exception as e:
|
98 |
-
error_message = f"An error occurred: {str(e)}"
|
99 |
logging.error(error_message)
|
100 |
yield error_message
|
101 |
|
@@ -103,6 +103,7 @@ def respond(
|
|
103 |
demo = gr.ChatInterface(
|
104 |
fn=respond,
|
105 |
additional_inputs=[
|
|
|
106 |
gr.Textbox(value="You are a friendly Chatbot.", label="System Message", lines=2),
|
107 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
|
108 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import logging
|
4 |
+
import json
|
5 |
+
import os
|
6 |
|
7 |
# Initialize the InferenceClient with the model ID from Hugging Face
|
8 |
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
|
|
14 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
15 |
)
|
16 |
|
17 |
+
API_KEYS_FILE = 'api_keys.json'
|
18 |
+
|
19 |
+
def load_api_keys():
|
20 |
+
"""Load API keys from the storage."""
|
21 |
+
if os.path.exists(API_KEYS_FILE):
|
22 |
+
with open(API_KEYS_FILE, 'r') as f:
|
23 |
+
return json.load(f)
|
24 |
+
return {}
|
25 |
+
|
26 |
+
def authenticate(api_key: str):
|
27 |
+
"""Authenticates the API key by checking against stored keys."""
|
28 |
+
api_keys = load_api_keys()
|
29 |
+
return api_key in api_keys.values()
|
30 |
+
|
31 |
def log_conversation(user_message, bot_response):
|
32 |
+
"""Logs the conversation between the user and the AI."""
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
logging.info(f"User: {user_message}")
|
34 |
logging.info(f"Bot: {bot_response}")
|
35 |
|
36 |
def respond(
|
37 |
+
api_key: str,
|
38 |
message: str,
|
39 |
history: list[tuple[str, str]],
|
40 |
system_message: str,
|
|
|
44 |
stop_sequence: str,
|
45 |
stream_response: bool,
|
46 |
):
|
47 |
+
"""Generates a response from the AI model based on the user's message and chat history."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Authenticate the API key
|
50 |
+
if not authenticate(api_key):
|
51 |
+
yield "Invalid API key. Access denied."
|
52 |
+
return
|
53 |
|
54 |
# Prepare the conversation history for the API call
|
55 |
messages = [{"role": "system", "content": system_message}]
|
|
|
95 |
yield response
|
96 |
|
97 |
except Exception as e:
|
98 |
+
error_message = f"An error occurred: {str(e)})"
|
99 |
logging.error(error_message)
|
100 |
yield error_message
|
101 |
|
|
|
103 |
demo = gr.ChatInterface(
|
104 |
fn=respond,
|
105 |
additional_inputs=[
|
106 |
+
gr.Textbox(value="", label="API Key", lines=1, type="password"),
|
107 |
gr.Textbox(value="You are a friendly Chatbot.", label="System Message", lines=2),
|
108 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens"),
|
109 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|