Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,26 +7,42 @@ import os
|
|
| 7 |
model_path = "rajj0/autotrain-phi3-midium-4k-godsent-orpo-6"
|
| 8 |
hf_token = os.getenv("HF_TOKEN") # Get the token from environment variables
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
if hf_token is None:
|
| 11 |
raise ValueError("HF_TOKEN environment variable not set")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
tokenizer
|
| 15 |
-
|
| 16 |
-
model_path,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Function to generate a response from the model
|
| 24 |
def generate_response(user_input):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Create the Gradio interface
|
| 32 |
iface = gr.Interface(
|
|
@@ -39,4 +55,6 @@ iface = gr.Interface(
|
|
| 39 |
|
| 40 |
# Launch the Gradio interface
|
| 41 |
if __name__ == "__main__":
|
| 42 |
-
|
|
|
|
|
|
|
|
|
| 7 |
model_path = "rajj0/autotrain-phi3-midium-4k-godsent-orpo-6"
|
| 8 |
hf_token = os.getenv("HF_TOKEN") # Get the token from environment variables
|
| 9 |
|
| 10 |
+
# Debugging: print the token to ensure it's being set
|
| 11 |
+
print(f"HF_TOKEN: {hf_token}")
|
| 12 |
+
|
| 13 |
if hf_token is None:
|
| 14 |
raise ValueError("HF_TOKEN environment variable not set")
|
| 15 |
|
| 16 |
+
try:
|
| 17 |
+
# Load the tokenizer and model with trust_remote_code=True
|
| 18 |
+
print("Loading tokenizer...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token=hf_token, trust_remote_code=True)
|
| 20 |
+
print("Tokenizer loaded successfully.")
|
| 21 |
+
|
| 22 |
+
print("Loading model...")
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
model_path,
|
| 25 |
+
device_map="auto",
|
| 26 |
+
torch_dtype='auto',
|
| 27 |
+
use_auth_token=hf_token,
|
| 28 |
+
trust_remote_code=True
|
| 29 |
+
).eval()
|
| 30 |
+
print("Model loaded successfully.")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error loading model or tokenizer: {e}")
|
| 33 |
+
raise
|
| 34 |
|
| 35 |
# Function to generate a response from the model
|
| 36 |
def generate_response(user_input):
|
| 37 |
+
try:
|
| 38 |
+
messages = [{"role": "user", "content": user_input}]
|
| 39 |
+
input_ids = tokenizer.apply_chat_template(conversation=messages, tokenize=True, add_generation_prompt=True, return_tensors='pt')
|
| 40 |
+
output_ids = model.generate(input_ids.to('cuda'))
|
| 41 |
+
response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 42 |
+
return response
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error generating response: {e}")
|
| 45 |
+
return "An error occurred while generating the response."
|
| 46 |
|
| 47 |
# Create the Gradio interface
|
| 48 |
iface = gr.Interface(
|
|
|
|
| 55 |
|
| 56 |
# Launch the Gradio interface
|
| 57 |
if __name__ == "__main__":
|
| 58 |
+
print("Launching Gradio interface...")
|
| 59 |
+
iface.launch()
|
| 60 |
+
print("Gradio interface launched.")
|