Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- huggingface_nlp.py +64 -0
- requirements.txt +5 -0
huggingface_nlp.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import transformers
|
| 2 |
+
transformers.__version__
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
torch.cuda.is_available()
|
| 6 |
+
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
generator = pipeline("text-generation")
|
| 10 |
+
|
| 11 |
+
generator = pipeline("text-generation", model = "Qwen/Qwen3-4B-Instruct-2507",
|
| 12 |
+
torch_dtype= "auto", device_map="auto")
|
| 13 |
+
|
| 14 |
+
messages = [
|
| 15 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
| 16 |
+
{"role": "user", "content": "Tell me a short joke"}
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
outputs = generator(
|
| 20 |
+
messages,
|
| 21 |
+
max_new_tokens = 100,
|
| 22 |
+
do_sample=True,
|
| 23 |
+
temperature = 0.7,
|
| 24 |
+
return_full_text = False)
|
| 25 |
+
|
| 26 |
+
print(outputs[0]["generated_text"])
|
| 27 |
+
|
| 28 |
+
messages = [
|
| 29 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
| 30 |
+
{"role": "user", "content": "Türkiyenin başkenti neresidir?"}
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
outputs = generator(
|
| 34 |
+
messages,
|
| 35 |
+
max_new_tokens = 100,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
temperature = 0.7,
|
| 38 |
+
return_full_text = False)
|
| 39 |
+
|
| 40 |
+
print(outputs[0]["generated_text"])
|
| 41 |
+
|
| 42 |
+
import gradio as gr
|
| 43 |
+
|
| 44 |
+
def generate_text(prompt):
|
| 45 |
+
messages = [
|
| 46 |
+
{"role": "system", "content": "You are a helpful AI assistant."},
|
| 47 |
+
{"role": "user", "content": prompt}]
|
| 48 |
+
outputs = generator(
|
| 49 |
+
messages,
|
| 50 |
+
max_new_tokens = 100,
|
| 51 |
+
do_sample=True,
|
| 52 |
+
temperature = 0.7,
|
| 53 |
+
return_full_text = False)
|
| 54 |
+
return outputs[0]["generated_text"]
|
| 55 |
+
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn= generate_text,
|
| 58 |
+
inputs = gr.Textbox(label="Give an input"),
|
| 59 |
+
outputs = gr.Textbox(label="Output"),
|
| 60 |
+
title = "Text Generation"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|
| 64 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|
| 4 |
+
accelerate
|
| 5 |
+
safetensors
|