Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,62 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load the model and tokenizer
|
| 6 |
-
model_name = "Qwen/Qwen2-Math-1.5B"
|
| 7 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
-
|
| 9 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
).to(device)
|
| 14 |
-
|
| 15 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
-
|
| 17 |
-
# Define a function for Gradio to handle user input
|
| 18 |
-
def solve_math(prompt):
|
| 19 |
-
|
| 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 |
-
# Create the Gradio interface
|
| 46 |
-
iface = gr.Interface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
|
| 54 |
# Launch the app
|
| 55 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
| 4 |
|
| 5 |
+
# Use a pipeline as a high-level helper
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
messages = [
|
| 9 |
+
{"role": "user", "content": "Who are you?"},
|
| 10 |
+
]
|
| 11 |
+
pipe = pipeline("text-generation", model="Qwen/Qwen2.5-Math-1.5B")
|
| 12 |
+
pipe(messages)
|
| 13 |
# Load the model and tokenizer
|
| 14 |
+
# model_name = "Qwen/Qwen2-Math-1.5B"
|
| 15 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
|
| 17 |
+
# model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
# model_name,
|
| 19 |
+
# torch_dtype="auto",
|
| 20 |
+
# device_map="auto"
|
| 21 |
+
# ).to(device)
|
| 22 |
+
|
| 23 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 24 |
+
|
| 25 |
+
# # Define a function for Gradio to handle user input
|
| 26 |
+
# def solve_math(prompt):
|
| 27 |
+
# messages = [
|
| 28 |
+
# {"role": "system", "content": "You are a helpful assistant."},
|
| 29 |
+
# {"role": "user", "content": prompt}
|
| 30 |
+
# ]
|
| 31 |
+
# text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 32 |
+
# model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
| 33 |
+
|
| 34 |
+
# generation_config = GenerationConfig(
|
| 35 |
+
# do_sample=False, # For greedy decoding
|
| 36 |
+
# max_new_tokens=512
|
| 37 |
+
# )
|
| 38 |
+
|
| 39 |
+
# generated_ids = model.generate(
|
| 40 |
+
# **model_inputs,
|
| 41 |
+
# generation_config=generation_config
|
| 42 |
+
# )
|
| 43 |
+
|
| 44 |
+
# # Remove the input tokens from the output
|
| 45 |
+
# generated_ids = [
|
| 46 |
+
# output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 47 |
+
# ]
|
| 48 |
+
|
| 49 |
+
# # Decode the generated output and return the result
|
| 50 |
+
# response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 51 |
+
# return response
|
| 52 |
+
|
| 53 |
+
# # Create the Gradio interface
|
| 54 |
+
# iface = gr.Interface(
|
| 55 |
+
# fn=solve_math, # Function to call
|
| 56 |
+
# inputs="text", # Text input for the user prompt
|
| 57 |
+
# outputs="text", # Text output for the model's response
|
| 58 |
+
# title="Math Solver", # App title
|
| 59 |
+
# description="Provide a math problem and the model will solve it."
|
| 60 |
+
# )
|
| 61 |
|
| 62 |
# Launch the app
|
| 63 |
if __name__ == "__main__":
|