Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
9 |
-
"AdnanRiaz107/CodePhi-3-Mini-1K4bit",
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained("AdnanRiaz107/CodePhi-3-Mini-1K4bit")
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
}
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|