Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Define the model name and cache path
|
6 |
+
model_name = 'MohamedRashad/Arabic-Orpo-Llama-3-8B-Instruct'
|
7 |
+
|
8 |
+
# Load the tokenizer and model
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
|
11 |
+
|
12 |
+
# Add a pad token if it does not exist
|
13 |
+
if tokenizer.pad_token is None:
|
14 |
+
tokenizer.pad_token = tokenizer.eos_token
|
15 |
+
|
16 |
+
def generate_response(input_text):
|
17 |
+
inputs = tokenizer(input_text, return_tensors='pt', padding=True, truncation=True, max_length=512)
|
18 |
+
inputs = {key: value.to("cuda" if torch.cuda.is_available() else "cpu") for key, value in inputs.items()}
|
19 |
+
|
20 |
+
with torch.no_grad():
|
21 |
+
outputs = model.generate(inputs['input_ids'], attention_mask=inputs['attention_mask'], max_length=50)
|
22 |
+
|
23 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
+
return response
|
25 |
+
|
26 |
+
interface = gr.Interface(fn=generate_response, inputs="text", outputs="text")
|
27 |
+
interface.launch(share=True)
|