Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor, AutoModel
|
4 |
+
import scipy.io.wavfile as wav
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load models and tokenizers
|
8 |
+
story_tokenizer = AutoTokenizer.from_pretrained("starmpcc/Asclepius-Llama3-8B", use_fast=False)
|
9 |
+
story_model = AutoModelForCausalLM.from_pretrained("starmpcc/Asclepius-Llama3-8B")
|
10 |
+
|
11 |
+
speech_processor = AutoProcessor.from_pretrained("suno/bark-small")
|
12 |
+
speech_model = AutoModel.from_pretrained("suno/bark-small")
|
13 |
+
|
14 |
+
# Function to generate story
|
15 |
+
def generate_story(prompt):
|
16 |
+
model_input = f"""You are an intelligent clinical language model.
|
17 |
+
Below is a snippet of patient's discharge summary and a following instruction from healthcare professional.
|
18 |
+
Write a response that appropriately completes the instruction.
|
19 |
+
The response should provide the accurate answer to the instruction, while being concise.
|
20 |
+
|
21 |
+
[Discharge Summary Begin]
|
22 |
+
{prompt}
|
23 |
+
[Discharge Summary End]
|
24 |
+
|
25 |
+
[Instruction Begin]
|
26 |
+
Generate a short story based on this prompt.
|
27 |
+
[Instruction End]
|
28 |
+
"""
|
29 |
+
|
30 |
+
input_ids = story_tokenizer(model_input, return_tensors="pt").input_ids
|
31 |
+
output = story_model.generate(input_ids, max_length=500)
|
32 |
+
return story_tokenizer.decode(output[0], skip_special_tokens=True)
|
33 |
+
|
34 |
+
# Function to generate speech
|
35 |
+
def generate_speech(text, speaker="v2/en_speaker_6"):
|
36 |
+
inputs = speech_processor(text, voice_preset=speaker, return_tensors="pt")
|
37 |
+
speech_values = speech_model.generate(**inputs, do_sample=True)
|
38 |
+
|
39 |
+
audio_array = speech_values.cpu().numpy().squeeze()
|
40 |
+
sample_rate = speech_model.generation_config.sample_rate
|
41 |
+
|
42 |
+
return (sample_rate, audio_array)
|
43 |
+
|
44 |
+
# Gradio interface function
|
45 |
+
def text_to_speech(prompt):
|
46 |
+
story = generate_story(prompt)
|
47 |
+
audio = generate_speech(story)
|
48 |
+
return story, audio
|
49 |
+
|
50 |
+
# Create Gradio interface
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=text_to_speech,
|
53 |
+
inputs=gr.Textbox(label="Enter story prompt"),
|
54 |
+
outputs=[
|
55 |
+
gr.Textbox(label="Generated Story"),
|
56 |
+
gr.Audio(label="Generated Speech")
|
57 |
+
],
|
58 |
+
title="Story Generator and Text-to-Speech",
|
59 |
+
description="Enter a prompt to generate a story, then convert it to speech."
|
60 |
+
)
|
61 |
+
|
62 |
+
# Launch the app
|
63 |
+
iface.launch()
|