aYeShaSiddiqA commited on
Commit
95154b2
Β·
verified Β·
1 Parent(s): 29fbe8e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+ import torch
4
+
5
+ model_name = "ajibawa-2023/Young-Children-Storyteller-Mistral-7B"
6
+
7
+ # Load tokenizer and model
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ # Try 8-bit quantization to reduce memory usage
11
+ try:
12
+ from transformers import BitsAndBytesConfig
13
+ bnb_config = BitsAndBytesConfig(load_in_8bit=True)
14
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", quantization_config=bnb_config)
15
+ except:
16
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
17
+
18
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
19
+
20
+ def generate_story(prompt):
21
+ outputs = generator(prompt, max_length=150, do_sample=True, temperature=0.8, top_p=0.9)
22
+ return outputs[0]['generated_text']
23
+
24
+ iface = gr.Interface(
25
+ fn=generate_story,
26
+ inputs=gr.Textbox(lines=3, placeholder="Enter your story prompt here..."),
27
+ outputs="text",
28
+ title="Young Children Storyteller",
29
+ description="Generate children's stories using Mistral 7B"
30
+ )
31
+
32
+ if __name__ == "__main__":
33
+ iface.launch()