Spaces:
Runtime error
Runtime error
holytinz278
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from database import db_connection
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
""
|
9 |
-
client = InferenceClient("WhiteRabbitNeo/WhiteRabbitNeo-13B-v1")
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
cursor = conn.cursor()
|
16 |
-
cursor.execute("SELECT message FROM system_messages WHERE name = 'MicroAI'")
|
17 |
-
result = cursor.fetchone()
|
18 |
-
cursor.close()
|
19 |
-
conn.close()
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
temperature=temperature,
|
51 |
-
top_p=top_p,
|
52 |
-
):
|
53 |
-
token = message.choices[0].delta.content
|
54 |
-
|
55 |
-
response += token
|
56 |
-
yield response
|
57 |
-
|
58 |
-
|
59 |
-
"""
|
60 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
61 |
-
"""
|
62 |
-
demo = gr.ChatInterface(
|
63 |
-
respond,
|
64 |
-
additional_inputs=[
|
65 |
-
gr.Textbox(value=fetch_system_message(), label="System message"), # Call the function to fetch system message
|
66 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
67 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
68 |
-
gr.Slider(
|
69 |
-
minimum=0.1,
|
70 |
-
maximum=1.0,
|
71 |
-
value=0.95,
|
72 |
-
step=0.05,
|
73 |
-
label="Top-p (nucleus sampling)",
|
74 |
-
),
|
75 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
)
|
77 |
|
|
|
78 |
if __name__ == "__main__":
|
79 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
3 |
|
4 |
+
# Load the model and tokenizer
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("WhiteRabbitNeo/WhiteRabbitNeo-13B-v1", trust_remote_code=True)
|
6 |
+
model = AutoModelForCausalLM.from_pretrained("WhiteRabbitNeo/WhiteRabbitNeo-13B-v1", trust_remote_code=True)
|
|
|
7 |
|
8 |
+
# Define a function to generate text with a system message
|
9 |
+
def generate_response(prompt, system_message, token):
|
10 |
+
# Combine system message and user prompt
|
11 |
+
full_prompt = f"{system_message}\n\n{prompt}"
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Tokenize input
|
14 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True)
|
15 |
+
|
16 |
+
# Generate a response
|
17 |
+
outputs = model.generate(
|
18 |
+
inputs["input_ids"],
|
19 |
+
max_length=300,
|
20 |
+
do_sample=True,
|
21 |
+
temperature=0.7,
|
22 |
+
top_k=50,
|
23 |
+
pad_token_id=tokenizer.eos_token_id # To prevent padding issues
|
24 |
+
)
|
25 |
+
|
26 |
+
# Decode the output text
|
27 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
|
29 |
+
# If a token is provided, add it to the response
|
30 |
+
if token:
|
31 |
+
response += f"\n\nToken Used: {token}"
|
32 |
+
|
33 |
+
return response
|
34 |
+
|
35 |
+
# Create a Gradio interface
|
36 |
+
interface = gr.Interface(
|
37 |
+
fn=generate_response,
|
38 |
+
inputs=[
|
39 |
+
gr.Textbox(lines=5, label="Prompt", placeholder="Type your prompt here..."),
|
40 |
+
gr.Textbox(lines=1, label="System Message", placeholder="System message (optional)"),
|
41 |
+
gr.Textbox(lines=1, label="Token", placeholder="Enter token (optional)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
],
|
43 |
+
outputs=gr.Textbox(label="Generated Response"),
|
44 |
+
title="WhiteRabbitNeo Enhanced Model",
|
45 |
+
description=(
|
46 |
+
"This app uses the WhiteRabbitNeo-13B-v1 model to generate text responses. "
|
47 |
+
"You can provide a system message, a prompt, and optionally include a token for custom usage."
|
48 |
+
),
|
49 |
)
|
50 |
|
51 |
+
# Launch the app
|
52 |
if __name__ == "__main__":
|
53 |
+
interface.launch()
|