sakurasaniya12345 commited on
Commit
0575df1
·
verified ·
1 Parent(s): 7016974

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -90
app.py CHANGED
@@ -1,92 +1,43 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- import os
4
-
5
- # Load Hugging Face API Token from Environment Variables
6
- HF_TOKEN = os.getenv("HF_TOKEN")
7
-
8
- # Initialize Mistral-7B Model
9
- client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1", token=HF_TOKEN)
10
-
11
- # Function to handle chat
12
- def respond(message, history, system_message, max_tokens, temperature, top_p):
13
- messages = [{"role": "system", "content": system_message}]
14
-
15
- for user_msg, bot_msg in history:
16
- if user_msg:
17
- messages.append({"role": "user", "content": user_msg})
18
- if bot_msg:
19
- messages.append({"role": "assistant", "content": bot_msg})
20
-
21
- messages.append({"role": "user", "content": message})
22
-
23
- response = ""
24
- for message in client.chat_completion(
25
- messages,
26
- max_tokens=max_tokens,
27
- stream=False, # Change to True if streaming works
28
- temperature=temperature,
29
- top_p=top_p,
30
- ):
31
- response += message.choices[0].delta.content
32
- yield response
33
-
34
- # Custom Styling for Dark Mode
35
- custom_css = """
36
- body {
37
- background-color: #121212;
38
- color: white;
39
- font-family: 'Arial', sans-serif;
40
- }
41
-
42
- .gradio-container {
43
- max-width: 700px;
44
- margin: auto;
45
- padding: 20px;
46
- background: #1E1E1E;
47
- border-radius: 10px;
48
- box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.2);
49
- }
50
-
51
- h1 {
52
- font-size: 24px;
53
- font-weight: bold;
54
- text-align: left;
55
- color: #00ccff;
56
- }
57
-
58
- h2 {
59
- text-align: center;
60
- font-size: 30px;
61
- font-weight: bold;
62
- color: white;
63
- }
64
-
65
- .watermark {
66
- text-align: center;
67
- font-size: 14px;
68
- color: gray;
69
- margin-top: 20px;
70
- }
71
- """
72
-
73
- # Gradio Chat Interface
74
- with gr.Blocks(css=custom_css) as demo:
75
- gr.Markdown("<h1>Mistral AI Chatbot</h1>") # Top left title
76
- gr.Markdown("<h2>How can I help you?</h2>") # Center title
77
-
78
- chatbot = gr.ChatInterface(
79
- respond,
80
- additional_inputs=[
81
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
82
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
83
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
84
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
85
- ],
86
- )
87
-
88
- gr.Markdown('<div class="watermark">Created by Rajma</div>')
89
-
90
- if __name__ == "__main__":
91
- demo.launch()
92
 
 
1
+ import torch
2
  import gradio as gr
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
+
5
+ model_name = "mistralai/Mistral-7B-Instruct-v0.1"
6
+
7
+ # Load tokenizer
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
9
+ tokenizer.padding_side = "left"
10
+ tokenizer.pad_token = tokenizer.eos_token
11
+
12
+ # Load model in 4-bit quantization
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ model_name,
15
+ device_map="auto",
16
+ load_in_4bit=True,
17
+ torch_dtype=torch.float16
18
+ )
19
+
20
+ # Create text generation pipeline
21
+ chatbot = pipeline(
22
+ "text-generation",
23
+ model=model,
24
+ tokenizer=tokenizer,
25
+ device=0,
26
+ pad_token_id=tokenizer.eos_token_id
27
+ )
28
+
29
+ # Function for chatting
30
+ def mistral_chat(user_input):
31
+ response = chatbot(user_input, max_new_tokens=200, temperature=0.7, do_sample=True)
32
+ return response[0]["generated_text"]
33
+
34
+ # Gradio interface
35
+ iface = gr.Interface(
36
+ fn=mistral_chat,
37
+ inputs="text",
38
+ outputs="text",
39
+ title="Mistral 7B Chatbot"
40
+ )
41
+
42
+ iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43