holytinz278 commited on
Commit
169d3e0
·
verified ·
1 Parent(s): 1f32b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -71
app.py CHANGED
@@ -1,79 +1,53 @@
1
  import gradio as gr
2
- import psycopg2
3
- from database import db_connection
4
- from huggingface_hub import InferenceClient
5
 
6
- """
7
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
8
- """
9
- client = InferenceClient("WhiteRabbitNeo/WhiteRabbitNeo-13B-v1")
10
 
11
- CREATOR_NAME = "Ridimz" # Define your creator name here
12
-
13
- def fetch_system_message():
14
- conn = db_connection()
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
- if result:
22
- return result[0] # Return the message associated with 'MicroAI'
23
- else:
24
- return f"You are Micro AI, created by {CREATOR_NAME}. Follow the instructions precisely."
25
-
26
- def respond(
27
- message,
28
- history: list[tuple[str, str]],
29
- system_message,
30
- max_tokens,
31
- temperature,
32
- top_p,
33
- ):
34
- messages = [{"role": "system", "content": system_message}]
35
-
36
- for val in history:
37
- if val[0]:
38
- messages.append({"role": "user", "content": val[0]})
39
- if val[1]:
40
- messages.append({"role": "assistant", "content": val[1]})
41
-
42
- messages.append({"role": "user", "content": message})
43
-
44
- response = ""
45
-
46
- for message in client.chat_completion(
47
- messages,
48
- max_tokens=max_tokens,
49
- stream=True,
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
- demo.launch()
 
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()