PuruAI commited on
Commit
6520b89
·
verified ·
1 Parent(s): ecf9f11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -1,29 +1,39 @@
1
  import os
 
2
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
3
 
4
  # Model configuration
5
- MODEL_ID = "PuruAI/Medini_Intelligence" # Your private model
6
  FALLBACK_MODEL = "gpt2"
7
-
8
- # Load Hugging Face token from secret
9
- HF_TOKEN = os.environ.get("HF_TOKEN")
10
 
11
  def load_model(model_id):
12
- """Load private model if possible, otherwise fallback to GPT-2."""
13
  try:
14
- print(f"Loading model: {model_id}")
15
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
16
- model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HF_TOKEN)
17
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
18
  except Exception as e:
19
  print(f"❌ Failed to load {model_id}: {e}")
20
- print(f"⏩ Falling back to {FALLBACK_MODEL}")
21
  return pipeline("text-generation", model=FALLBACK_MODEL)
22
 
23
  # Initialize pipeline
24
  generator = load_model(MODEL_ID)
25
 
26
- # Test generation
27
- prompt = "Once upon a time"
28
- output = generator(prompt, max_new_tokens=50)
29
- print(output)
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import gradio as gr
3
  from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
4
 
5
  # Model configuration
6
+ MODEL_ID = "PuruAI/Medini_Intelligence"
7
  FALLBACK_MODEL = "gpt2"
8
+ HF_TOKEN = os.getenv("HF_TOKEN") # must be set in your env/secrets
 
 
9
 
10
  def load_model(model_id):
11
+ """Load Medini if available, otherwise fallback to GPT-2."""
12
  try:
13
+ print(f"🔹 Loading model: {model_id}")
14
+ tokenizer = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
15
+ model = AutoModelForCausalLM.from_pretrained(model_id, token=HF_TOKEN)
16
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
17
  except Exception as e:
18
  print(f"❌ Failed to load {model_id}: {e}")
19
+ print("⏩ Falling back to GPT-2 (no token needed)")
20
  return pipeline("text-generation", model=FALLBACK_MODEL)
21
 
22
  # Initialize pipeline
23
  generator = load_model(MODEL_ID)
24
 
25
+ def generate_text(prompt):
26
+ outputs = generator(prompt, max_length=200, num_return_sequences=1)
27
+ return outputs[0]["generated_text"]
28
+
29
+ # Gradio UI
30
+ iface = gr.Interface(
31
+ fn=generate_text,
32
+ inputs="text",
33
+ outputs="text",
34
+ title="Medini Intelligence",
35
+ description="Custom AI Agent with fallback to GPT-2"
36
+ )
37
+
38
+ if __name__ == "__main__":
39
+ iface.launch()