AdnanRiaz107 commited on
Commit
49383f2
·
verified ·
1 Parent(s): ad82e94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -36
app.py CHANGED
@@ -1,42 +1,67 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
- import torch
5
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
6
-
7
- torch.random.manual_seed(0)
8
- model = AutoModelForCausalLM.from_pretrained(
9
- "AdnanRiaz107/CodePhi-3-Mini-1K4bit",
10
- device_map="cuda",
11
- torch_dtype="auto",
12
- trust_remote_code=True,
13
- attn_implementation='eager',
14
- )
15
-
16
- tokenizer = AutoTokenizer.from_pretrained("AdnanRiaz107/CodePhi-3-Mini-1K4bit")
17
-
18
- messages = [
19
- {"role": "system", "content": "You are a helpful AI assistant."},
20
- {"role": "user", "content": "Can you provide ways to eat combinations of bananas and dragonfruits?"},
21
- {"role": "assistant", "content": "Sure! Here are some ways to eat bananas and dragonfruits together: 1. Banana and dragonfruit smoothie: Blend bananas and dragonfruits together with some milk and honey. 2. Banana and dragonfruit salad: Mix sliced bananas and dragonfruits together with some lemon juice and honey."},
22
- {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
23
- ]
24
-
25
- pipe = pipeline(
26
- "text-generation",
27
- model=model,
28
- tokenizer=tokenizer,
29
- )
30
-
31
- generation_args = {
32
- "max_new_tokens": 500,
33
- "return_full_text": False,
34
- "temperature": 0.0,
35
- "do_sample": False,
36
- }
37
-
38
- output = pipe(messages, **generation_args)
39
- print(output[0]['generated_text'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  if __name__ == "__main__":
42
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
 
6
+ # Set the random seed for reproducibility
7
+ torch.random.manual_seed(0)
8
+
9
+ # Load the model without specifying 'device_map' for CPU usage
10
+ model = AutoModelForCausalLM.from_pretrained(
11
+ "AdnanRiaz107/CodePhi-3-Mini-1K4bit",
12
+ torch_dtype="auto", # Use auto for dtype selection
13
+ trust_remote_code=True,
14
+ attn_implementation='eager', # Keep this if you want to use 'eager'
15
+ )
16
+
17
+ # Load the tokenizer
18
+ tokenizer = AutoTokenizer.from_pretrained("AdnanRiaz107/CodePhi-3-Mini-1K4bit")
19
+
20
+ # Create a text generation pipeline
21
+ pipe = pipeline(
22
+ "text-generation",
23
+ model=model,
24
+ tokenizer=tokenizer,
25
+ )
26
+
27
+ # Generation arguments
28
+ generation_args = {
29
+ "max_new_tokens": 500,
30
+ "return_full_text": False,
31
+ "temperature": 0.0,
32
+ "do_sample": False,
33
+ }
34
+
35
+ # Gradio interface function
36
+ def generate_response(input_text):
37
+ # Prepare the input for the model
38
+ messages = [{"role": "user", "content": input_text}]
39
+
40
+ # Generate output
41
+ output = pipe(messages, **generation_args)
42
+ return output[0]['generated_text']
43
+
44
+ # Create Gradio demo interface
45
+ demo = gr.Interface(
46
+ fn=generate_response,
47
+ inputs=gr.Textbox(
48
+ lines=2,
49
+ placeholder="Enter your question here...",
50
+ label="Your Input",
51
+ ),
52
+ outputs=gr.Textbox(
53
+ label="Model Response",
54
+ placeholder="Response will be displayed here...",
55
+ ),
56
+ title="AI Assistant for Python Code Generation",
57
+ description="Ask any question or request information, and the AI assistant will provide a response. Try asking about recipes, solving equations, or general inquiries.",
58
+ examples=[
59
+ ["Can you provide ways to eat combinations of bananas and dragonfruits?"],
60
+ ["What about solving the equation 2x + 3 = 7?"],
61
+ ["Tell me about the history of the internet."],
62
+ ],
63
+ theme="default" # You can change the theme to "compact", "default", "huggingface", etc.
64
+ )
65
 
66
  if __name__ == "__main__":
67
  demo.launch()