Hermit11 commited on
Commit
2114976
·
verified ·
1 Parent(s): 90635fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,26 +1,25 @@
1
  import gradio as gr
2
  import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor, AutoModel, BitsAndBytesConfig
4
  import scipy.io.wavfile as wav
5
  import numpy as np
 
 
 
 
 
 
6
 
7
  # Check for GPU availability
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
- # Quantization config
11
  if device == "cuda":
12
  quantization_config = BitsAndBytesConfig(load_in_8bit=True)
13
  else:
14
  quantization_config = None
15
 
16
- # Load models and tokenizers
17
- story_tokenizer = AutoTokenizer.from_pretrained("starmpcc/Asclepius-Llama3-8B", use_fast=False)
18
- story_model = AutoModelForCausalLM.from_pretrained(
19
- "starmpcc/Asclepius-Llama3-8B",
20
- device_map="auto" if device == "cuda" else None,
21
- quantization_config=quantization_config
22
- )
23
-
24
  speech_processor = AutoProcessor.from_pretrained("suno/bark-small")
25
  speech_model = AutoModel.from_pretrained(
26
  "suno/bark-small",
@@ -28,27 +27,34 @@ speech_model = AutoModel.from_pretrained(
28
  quantization_config=quantization_config
29
  )
30
 
31
- # Move models to the appropriate device
32
- story_model.to(device)
33
  speech_model.to(device)
34
 
35
- # Function to generate story
36
  def generate_story(prompt):
37
- model_input = f"""You are an intelligent clinical language model.
38
- Below is a snippet of patient's discharge summary and a following instruction from healthcare professional.
39
- Write a response that appropriately completes the instruction.
40
- The response should provide the accurate answer to the instruction, while being concise.
41
- [Discharge Summary Begin]
 
42
  {prompt}
43
- [Discharge Summary End]
 
44
  [Instruction Begin]
45
- Generate a short story based on this prompt.
46
- [Instruction End]
47
  """
48
 
49
- input_ids = story_tokenizer(model_input, return_tensors="pt").input_ids.to(device)
50
- output = story_model.generate(input_ids, max_length=500)
51
- return story_tokenizer.decode(output[0], skip_special_tokens=True)
 
 
 
 
 
 
52
 
53
  # Function to generate speech
54
  def generate_speech(text, speaker="v2/en_speaker_6"):
@@ -75,7 +81,7 @@ iface = gr.Interface(
75
  gr.Audio(label="Generated Speech")
76
  ],
77
  title="Story Generator and Text-to-Speech",
78
- description="Enter a prompt to generate a story, then convert it to speech."
79
  )
80
 
81
  # Launch the app
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoProcessor, AutoModel, BitsAndBytesConfig
4
  import scipy.io.wavfile as wav
5
  import numpy as np
6
+ import os
7
+ import openai
8
+
9
+ # Set OpenAI API key (preferably use environment variables)
10
+ os.environ["OPENAI_API_KEY"] = "sk-proj-hx8b2Z3ja8dyzDH1DtCOT3BlbkFJPV804EO36VB4LbflIUa7"
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
12
 
13
  # Check for GPU availability
14
  device = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
+ # Quantization config for speech model
17
  if device == "cuda":
18
  quantization_config = BitsAndBytesConfig(load_in_8bit=True)
19
  else:
20
  quantization_config = None
21
 
22
+ # Load speech model and processor
 
 
 
 
 
 
 
23
  speech_processor = AutoProcessor.from_pretrained("suno/bark-small")
24
  speech_model = AutoModel.from_pretrained(
25
  "suno/bark-small",
 
27
  quantization_config=quantization_config
28
  )
29
 
30
+ # Move speech model to the appropriate device
 
31
  speech_model.to(device)
32
 
33
+ # Function to generate story using GPT
34
  def generate_story(prompt):
35
+ model_input = f"""You are a creative and educational storyteller for school-going children.
36
+ Your task is to create an engaging, age-appropriate story that both entertains and teaches valuable lessons.
37
+ Use the following prompt as inspiration for your story, but feel free to be imaginative and expand upon it.
38
+ Remember to include educational elements that children can learn from, such as historical facts, scientific concepts, moral lessons, or cultural insights.
39
+
40
+ [Story Prompt Begin]
41
  {prompt}
42
+ [Story Prompt End]
43
+
44
  [Instruction Begin]
45
+ Generate a short, creative, and educational story based on this prompt. The story should be suitable for school-going children, entertaining, and contain clear learning points.
46
+ [Instruction End]
47
  """
48
 
49
+ response = openai.ChatCompletion.create(
50
+ model="gpt-3.5-turbo", # or "gpt-4" if you have access
51
+ messages=[
52
+ {"role": "system", "content": "You are a skilled storyteller who creates educational and engaging stories for children."},
53
+ {"role": "user", "content": model_input}
54
+ ]
55
+ )
56
+
57
+ return response.choices[0].message['content']
58
 
59
  # Function to generate speech
60
  def generate_speech(text, speaker="v2/en_speaker_6"):
 
81
  gr.Audio(label="Generated Speech")
82
  ],
83
  title="Story Generator and Text-to-Speech",
84
+ description="Enter a prompt to generate a story using GPT, then convert it to speech."
85
  )
86
 
87
  # Launch the app