nikunjcepatel commited on
Commit
b3fecc1
·
verified ·
1 Parent(s): ee072ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from huggingface_hub import login
4
+ import os
5
+
6
+ # Retrieve the Hugging Face token from the Space secrets
7
+ token = os.getenv("HF_TOKEN")
8
+
9
+ # Log in using the token
10
+ login(token=token)
11
+
12
+
13
+ # Load model and tokenizer
14
+ model_name = "openai-community/gpt2" #"meta-llama/Llama-3.2-3B" # Replace with the correct model name if necessary
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+ model = AutoModelForCausalLM.from_pretrained(model_name)
17
+
18
+ # Define inference function
19
+ def generate_text(input_text):
20
+ inputs = tokenizer(input_text, return_tensors="pt")
21
+ outputs = model.generate(inputs["input_ids"], max_length=50, num_return_sequences=1)
22
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
23
+ return response
24
+
25
+ # Create Gradio interface
26
+ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
27
+
28
+ # Launch the interface
29
+ iface.launch()