24Sureshkumar commited on
Commit
639070c
·
verified ·
1 Parent(s): c412cd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -6
app.py CHANGED
@@ -4,6 +4,17 @@ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
4
  from diffusers import StableDiffusionPipeline
5
  import torch
6
  import re
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Initialize models
9
  def load_models():
@@ -13,14 +24,16 @@ def load_models():
13
  model="facebook/nllb-200-distilled-600M",
14
  src_lang="tam_Taml",
15
  tgt_lang="eng_Latn",
16
- device=0 if torch.cuda.is_available() else -1
 
17
  )
18
 
19
  # Text generation pipeline
20
  text_generator = pipeline(
21
  "text-generation",
22
  model="gpt2-medium",
23
- device=0 if torch.cuda.is_available() else -1
 
24
  )
25
 
26
  # Image generation pipeline
@@ -28,15 +41,22 @@ def load_models():
28
  image_pipe = StableDiffusionPipeline.from_pretrained(
29
  "runwayml/stable-diffusion-v1-5",
30
  torch_dtype=torch.float16,
31
- revision="fp16"
 
32
  ).to("cuda")
33
  else:
34
- image_pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
 
 
 
35
 
36
  return translator, text_generator, image_pipe
37
 
38
  # Load models at startup
39
- translator, text_generator, image_pipe = load_models()
 
 
 
40
 
41
  def clean_text(text):
42
  """Remove special characters and truncate to complete sentences"""
@@ -112,7 +132,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
112
  clear_btn = gr.Button("துடைத்து துவக்கவும்")
113
 
114
  def clear_all():
115
- return {"": ""}, "", "", None, ""
116
 
117
  submit_btn.click(
118
  fn=process_content,
 
4
  from diffusers import StableDiffusionPipeline
5
  import torch
6
  import re
7
+ import os
8
+ from huggingface_hub import login
9
+
10
+ # Enter your Hugging Face token here (or set as environment variable)
11
+ HF_TOKEN = "your_hf_token_here" # Replace with your actual token
12
+
13
+ # Authenticate with Hugging Face Hub
14
+ if HF_TOKEN:
15
+ login(token=HF_TOKEN)
16
+ else:
17
+ raise ValueError("Hugging Face token not provided!")
18
 
19
  # Initialize models
20
  def load_models():
 
24
  model="facebook/nllb-200-distilled-600M",
25
  src_lang="tam_Taml",
26
  tgt_lang="eng_Latn",
27
+ device=0 if torch.cuda.is_available() else -1,
28
+ use_auth_token=HF_TOKEN # Token added here
29
  )
30
 
31
  # Text generation pipeline
32
  text_generator = pipeline(
33
  "text-generation",
34
  model="gpt2-medium",
35
+ device=0 if torch.cuda.is_available() else -1,
36
+ use_auth_token=HF_TOKEN # Token added here
37
  )
38
 
39
  # Image generation pipeline
 
41
  image_pipe = StableDiffusionPipeline.from_pretrained(
42
  "runwayml/stable-diffusion-v1-5",
43
  torch_dtype=torch.float16,
44
+ revision="fp16",
45
+ use_auth_token=HF_TOKEN # Token added here
46
  ).to("cuda")
47
  else:
48
+ image_pipe = StableDiffusionPipeline.from_pretrained(
49
+ "runwayml/stable-diffusion-v1-5",
50
+ use_auth_token=HF_TOKEN # Token added here
51
+ )
52
 
53
  return translator, text_generator, image_pipe
54
 
55
  # Load models at startup
56
+ try:
57
+ translator, text_generator, image_pipe = load_models()
58
+ except Exception as e:
59
+ raise RuntimeError(f"Model loading failed: {str(e)}")
60
 
61
  def clean_text(text):
62
  """Remove special characters and truncate to complete sentences"""
 
132
  clear_btn = gr.Button("துடைத்து துவக்கவும்")
133
 
134
  def clear_all():
135
+ return "", "", "", None, ""
136
 
137
  submit_btn.click(
138
  fn=process_content,